Completed
Push — development ( 3256d6...e60c50 )
by Thomas
17s queued 10s
created

local/devel/Repositories/WsSessionsRepository.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
use Doctrine\DBAL\Connection;
4
use Oc\Repository\Exception\RecordAlreadyExistsException;
5
use Oc\Repository\Exception\RecordNotFoundException;
6
use Oc\Repository\Exception\RecordNotPersistedException;
7
use Oc\Repository\Exception\RecordsNotFoundException;
8
9
class WsSessionsRepository
10
{
11
    const TABLE = 'ws_sessions';
12
13
    /** @var Connection */
14
    private $connection;
15
16
    public function __construct(Connection $connection)
17
    {
18
        $this->connection = $connection;
19
    }
20
21
    /**
22
     * @return WsSessionsEntity[]
23
     */
24
    public function fetchAll()
25
    {
26
        $statement = $this->connection->createQueryBuilder()
27
            ->select('*')
28
            ->from(self::TABLE)
29
            ->execute();
30
31
        $result = $statement->fetchAll();
32
33
        if ($statement->rowCount() === 0) {
34
            throw new RecordsNotFoundException('No records found');
35
        }
36
37
        $records = [];
38
39
        foreach ($result as $item) {
40
            $records[] = $this->getEntityFromDatabaseArray($item);
41
        }
42
43
        return $records;
44
    }
45
46
    /**
47
     * @return WsSessionsEntity
48
     */
49
    public function fetchOneBy(array $where = [])
50
    {
51
        $queryBuilder = $this->connection->createQueryBuilder()
52
            ->select('*')
53
            ->from(self::TABLE)
54
            ->setMaxResults(1);
55
56
        if (count($where) > 0) {
57
            foreach ($where as $column => $value) {
58
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
59
            }
60
        }
61
62
        $statement = $queryBuilder->execute();
63
64
        $result = $statement->fetch();
65
66
        if ($statement->rowCount() === 0) {
67
            throw new RecordNotFoundException('Record with given where clause not found');
68
        }
69
70
        return $this->getEntityFromDatabaseArray($result);
71
    }
72
73
    /**
74
     * @return WsSessionsEntity[]
75
     */
76
    public function fetchBy(array $where = [])
77
    {
78
        $queryBuilder = $this->connection->createQueryBuilder()
79
            ->select('*')
80
            ->from(self::TABLE);
81
82
        if (count($where) > 0) {
83
            foreach ($where as $column => $value) {
84
                $queryBuilder->andWhere($column . ' = ' . $queryBuilder->createNamedParameter($value));
85
            }
86
        }
87
88
        $statement = $queryBuilder->execute();
89
90
        $result = $statement->fetchAll();
91
92
        if ($statement->rowCount() === 0) {
93
            throw new RecordsNotFoundException('No records with given where clause found');
94
        }
95
96
        $entities = [];
97
98
        foreach ($result as $item) {
99
            $entities[] = $this->getEntityFromDatabaseArray($item);
100
        }
101
102
        return $entities;
103
    }
104
105
    /**
106
     * @return WsSessionsEntity
107
     */
108
    public function create(WsSessionsEntity $entity)
109
    {
110
        if (!$entity->isNew()) {
111
            throw new RecordAlreadyExistsException('The entity does already exist.');
112
        }
113
114
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
115
116
        $this->connection->insert(
117
            self::TABLE,
118
            $databaseArray
119
        );
120
121
        $entity->id = (int) $this->connection->lastInsertId();
0 ignored issues
show
Documentation Bug introduced by
The property $id was declared of type string, but (int) $this->connection->lastInsertId() is of type integer. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
122
123
        return $entity;
124
    }
125
126
    /**
127
     * @return WsSessionsEntity
128
     */
129
    public function update(WsSessionsEntity $entity)
130
    {
131
        if ($entity->isNew()) {
132
            throw new RecordNotPersistedException('The entity does not exist.');
133
        }
134
135
        $databaseArray = $this->getDatabaseArrayFromEntity($entity);
136
137
        $this->connection->update(
138
            self::TABLE,
139
            $databaseArray,
140
            ['id' => $entity->id]
141
        );
142
143
        return $entity;
144
    }
145
146
    /**
147
     * @return WsSessionsEntity
148
     */
149
    public function remove(WsSessionsEntity $entity)
150
    {
151
        if ($entity->isNew()) {
152
            throw new RecordNotPersistedException('The entity does not exist.');
153
        }
154
155
        $this->connection->delete(
156
            self::TABLE,
157
            ['id' => $entity->id]
158
        );
159
160
        $entity->cacheId = null;
0 ignored issues
show
The property cacheId does not seem to exist in WsSessionsEntity.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
161
162
        return $entity;
163
    }
164
165
    /**
166
     * @return []
0 ignored issues
show
The doc-type [] could not be parsed: Unknown type name "" at position 0. [(view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
167
     */
168
    public function getDatabaseArrayFromEntity(WsSessionsEntity $entity)
169
    {
170
        return [
171
            'id' => $entity->id,
172
            'user_id' => $entity->userId,
173
            'date_created' => $entity->dateCreated,
174
            'last_usage' => $entity->lastUsage,
175
            'valid' => $entity->valid,
176
            'closed' => $entity->closed,
177
        ];
178
    }
179
180
    /**
181
     * @return WsSessionsEntity
182
     */
183
    public function getEntityFromDatabaseArray(array $data)
184
    {
185
        $entity = new WsSessionsEntity();
186
        $entity->id = (string) $data['id'];
187
        $entity->userId = (int) $data['user_id'];
188
        $entity->dateCreated = new DateTime($data['date_created']);
189
        $entity->lastUsage = new DateTime($data['last_usage']);
190
        $entity->valid = (int) $data['valid'];
191
        $entity->closed = (int) $data['closed'];
192
193
        return $entity;
194
    }
195
}
196