Passed
Branch master (ecaff5)
by Joao
02:24
created

UserRepository::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 9.488
c 0
b 0
f 0
cc 3
nc 4
nop 2
1
<?php
2
3
namespace RestTemplate\Repository;
4
5
use ByJG\AnyDataset\Db\DbDriverInterface;
6
use ByJG\Authenticate\Definition\UserDefinition;
7
use ByJG\MicroOrm\Exception\InvalidArgumentException;
8
use ByJG\MicroOrm\Exception\OrmModelInvalidException;
9
use ByJG\MicroOrm\Mapper;
10
use ByJG\MicroOrm\Repository;
11
12
Class UserRepository extends BaseRepository
13
{
14
    /**
15
     * UserRepository constructor.
16
     * @param DbDriverInterface $dbDriver
17
     * @param UserDefinition|null $userTable
18
     * @throws InvalidArgumentException
19
     * @throws OrmModelInvalidException
20
     */
21
    public function __construct(
22
        DbDriverInterface $dbDriver,
23
        UserDefinition $userTable = null
24
    ) {
25
        if (empty($userTable)) {
26
            $userTable = new UserDefinition();
27
        }
28
29
        $userMapper = new Mapper(
30
            $userTable->model(),
31
            $userTable->table(),
32
            $userTable->getUserid()
33
        );
34
35
        $propertyDefinition = $userTable->toArray();
36
37
        foreach ($propertyDefinition as $property => $map) {
38
            $userMapper->addFieldMap(
39
                $property,
40
                $map,
41
                $userTable->getClosureForUpdate($property),
42
                $userTable->getClosureForSelect($property)
43
            );
44
        }
45
46
        $this->repository = new Repository($dbDriver, $userMapper);
47
    }
48
}
49