DatabaseGroupsRepository.php 3.5 KB
Newer Older
1
2
3
4
5
<?php


namespace Source\Gateways\Groups;

6
use Source\Entities\Group;
Jacob Priddy's avatar
Jacob Priddy committed
7
use Source\Sanitize\CastsTo;
8
use Illuminate\Support\Facades\Log;
9
use Source\Gateways\PostgresSQLCodes;
10
11
use Illuminate\Database\QueryException;
use Source\Exceptions\EntityExistsException;
12

13
14
class DatabaseGroupsRepository implements GroupsRepository
{
Jacob Priddy's avatar
Jacob Priddy committed
15
    use CastsTo;
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37

    /**
     * @param \App\Group             $dbGroup
     * @param \Source\Entities\Group $group
     * @throws \Source\Exceptions\EntityExistsException
     */
    protected function saveGroup(\App\Group $dbGroup, Group $group): void
    {
        $dbGroup = self::fillBasicAttrs($group, $dbGroup);

        try {
            $dbGroup->save();
        } catch (QueryException $e) {
            if ($e->getCode() === PostgresSQLCodes::UNIQUE_CONSTRAINT_VIOLATION) {
                throw new EntityExistsException('Cannot save group. Group already exists.');
            }

            Log::error('Failed saving group: ' . $e);

            throw $e;
        }
    }
Jacob Priddy's avatar
Jacob Priddy committed
38

39
40
41
42
43
    /**
     * @inheritDoc
     */
    public function get(string $groupId): ?Group
    {
Jacob Priddy's avatar
Jacob Priddy committed
44
        /** @var \App\Group|null $group */
45
        $group = \App\Group::query()->find(self::castToInt($groupId));
46
47
48
49
50

        if (!$group) {
            return null;
        }

51
        return self::makeGroupFromDbGroup($group);
52
53
    }

54
55
56
57
    /**
     * @param \App\Group $group
     * @return \Source\Entities\Group
     */
58
    public static function makeGroupFromDbGroup(\App\Group $group): Group
59
60
    {
        return new Group(
Jacob Priddy's avatar
Jacob Priddy committed
61
62
63
64
65
            $group->getAttribute('id'),
            $group->getAttribute('title'),
            $group->getAttribute('description'),
            $group->getAttribute('created_at'),
            $group->getAttribute('updated_at')
66
67
68
        );
    }

69
70
71
    /**
     * @inheritDoc
     */
72
    public function search(?string $query = null): array
73
    {
74
75
        if ($query) {
            $groups = \App\Group::query()
76
77
                ->where('title', 'ILIKE', "%$query%")
                ->orWhere('description', 'ILIKE', "%$query%")
78
79
80
81
                ->get()->values()->all();
        } else {
            $groups = \App\Group::all()->values()->all();
        }
82
83
84
85
86
87
88
89
90

        return array_map(function (\App\Group $group) {
            return $this->makeGroupFromDbGroup($group);
        }, $groups);
    }

    /**
     * @inheritDoc
     */
91
    public function create(Group $group): Group
92
    {
93
        $newGroup = new \App\Group();
94

95
        $this->saveGroup($newGroup, $group);
96

97
        return self::makeGroupFromDbGroup($newGroup);
98
99
    }

100
    protected static function fillBasicAttrs(Group $group, \App\Group $dbGroup): \App\Group
101
    {
Jacob Priddy's avatar
Jacob Priddy committed
102
103
        $dbGroup->setAttribute('title', $group->getTitle());
        $dbGroup->setAttribute('description', $group->getDescription());
104
105
106
107

        return $dbGroup;
    }

108
109
110
111
112
    /**
     * @inheritDoc
     */
    public function update(string $groupId, Group $group): ?Group
    {
Jacob Priddy's avatar
Jacob Priddy committed
113
        /** @var \App\Group|null $dbGroup */
114
        $dbGroup = \App\Group::query()->find(self::castToInt($groupId));
115
116
117
118
119

        if (!$dbGroup) {
            return null;
        }

120
        $this->saveGroup($dbGroup, $group);
121

122
        return self::makeGroupFromDbGroup($dbGroup);
123
124
125
126
127
128
129
    }

    /**
     * @inheritDoc
     */
    public function delete(string $groupId): bool
    {
130
        return \App\Group::destroy(self::castToInt($groupId));
131
132
133
134
135
    }

    /**
     * @inheritDoc
     */
136
    public function exists(Group $group): bool
137
    {
Jacob Priddy's avatar
Jacob Priddy committed
138
        return \App\Group::query()->where('title', $group->getTitle())->orWhere('id', $group->getId())->first() !== null;
139
    }
140
}