Completed
Pull Request — development (#801)
by
unknown
04:18
created

CachesRepository::fetchOneById()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
3
namespace Oc\Repository;
4
5
use Doctrine\DBAL\Connection;
6
use Oc\Entity\CachesEntity;
7
use Oc\Repository\Exception\RecordAlreadyExistsException;
8
use Oc\Repository\Exception\RecordNotFoundException;
9
use Oc\Repository\Exception\RecordNotPersistedException;
10
use Oc\Repository\Exception\RecordsNotFoundException;
11
12
class CachesRepository
13
{
14
    /**
15
     * Database table name that this repository maintains.
16
     *
17
     * @var string
18
     */
19
    public const TABLE = 'caches';
20
21
    /**
22
     * @var Connection
23
     */
24
    private $connection;
25
    /**
26
     * @var SecurityRolesRepository
27
     */
28
    private $securityRolesRepository; //??
29
30
    public function __construct(Connection $connection, SecurityRolesRepository  $securityRolesRepository) //??
31
    {
32
        $this->connection = $connection;
33
        $this->securityRolesRepository = $securityRolesRepository;
34
    }
35
36
    /**
37
     * Fetches all caches.
38
     *
39
     * @throws RecordsNotFoundException Thrown when no records are found
40
     * @return UserEntity[]
41
     */
42
    /**
43
     * Fetches a cache by its id.
44
     *
45
     * @throws RecordNotFoundException Thrown when the request record is not found
46
     */
47
    public function fetchOneById(int $id): UserEntity
48
    {
49
        $statement = $this->connection->createQueryBuilder()
50
            ->select('*')
51
            ->from(self::TABLE)
52
            ->where('cache_id = :id')
53
            ->setParameter(':id', $id)
54
            ->execute();
55
56
        $result = $statement->fetch();
57
58
        if ($statement->rowCount() === 0) {
59
            throw new RecordNotFoundException(sprintf(
60
                'Record with id #%s not found',
61
                $id
62
            ));
63
        }
64
65
        return $this->getEntityFromDatabaseArray($result);
66
    }
67
68
    /**
69
     * Prepares database array from properties.
70
     */
71
    public function getEntityFromDatabaseArray(array $data): CachesEntity
72
    {
73
        $entity = new CachesEntity();
74
        $entity->id = (int) $data['cache_id'];
75
        $entity->name = $data['name'];
76
        $entity->wp_gc = $data['wp_gc'];
77
        $entity->wp_oc = $data['wp_oc'];
78
        $entity->latitude = (double) $data['latitude'];
79
        $entity->longitude = (double) $data['longitude'];
80
81
        return $entity;
82
    }
83
}
84