Completed
Push — master ( 0d4b62...49eab6 )
by Adrien
07:35
created

AccountRepository::getOneById()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Application\Repository;
6
7
use Application\DBAL\Types\AccountTypeType;
8
use Application\Model\Account;
9
use Application\Model\User;
10
11
class AccountRepository extends AbstractRepository implements LimitedAccessSubQueryInterface
12
{
13
    private const PARENT_ACCOUNT_ID_FOR_USER = 10011;
14
15
    /**
16
     * Returns pure SQL to get ID of all objects that are accessible to given user.
17
     *
18
     * @param null|User $user
19
     *
20
     * @return string
21
     */
22 12
    public function getAccessibleSubQuery(?User $user): string
23
    {
24 12
        if (!$user) {
25 2
            return '-1';
26
        }
27
28 10
        if (in_array($user->getRole(), [User::ROLE_RESPONSIBLE, User::ROLE_ADMINISTRATOR], true)) {
29 2
            return $this->getAllIdsQuery();
30
        }
31
32 8
        return 'SELECT id FROM account WHERE owner_id = ' . $user->getId();
33
    }
34
35
    /**
36
     * Unsecured way to get a account from its ID.
37
     *
38
     * This should only be used in tests or controlled environment.
39
     *
40
     * @param int $id
41
     *
42
     * @return null|Account
43
     */
44 2
    private function getOneById(int $id): ?Account
45
    {
46 2
        $this->getAclFilter()->setEnabled(false);
47 2
        $account = $this->findOneById($id);
48 2
        $this->getAclFilter()->setEnabled(true);
49
50 2
        return $account;
51
    }
52
53
    /**
54
     * This will return, and potentially create, an account for the given user
55
     *
56
     * @param User $user
57
     *
58
     * @return Account
59
     */
60 5
    public function getOrCreate(User $user): Account
61
    {
62 5
        $account = $this->findOneByOwner($user);
0 ignored issues
show
Bug introduced by
The method findOneByOwner() does not exist on Application\Repository\AccountRepository. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
        /** @scrutinizer ignore-call */ 
63
        $account = $this->findOneByOwner($user);
Loading history...
63 5
        if (!$account) {
64 2
            $account = new Account();
65 2
            $this->getEntityManager()->persist($account);
66 2
            $account->setOwner($user);
67 2
            $account->setType(AccountTypeType::LIABILITY);
68 2
            $account->setName($user->getName());
69
70 2
            $maxCode = $this->getEntityManager()->getConnection()->fetchColumn('SELECT MAX(code) FROM account WHERE parent_id = ' . self::PARENT_ACCOUNT_ID_FOR_USER);
71 2
            $newCode = ++$maxCode;
72 2
            $account->setCode((string) $newCode);
73
74 2
            $parent = $this->getOneById(self::PARENT_ACCOUNT_ID_FOR_USER);
75 2
            if (!$parent) {
76
                throw new \Exception('Cannot find parent account for creation of user account');
77
            }
78 2
            $account->setParent($parent);
79
        }
80
81 5
        return $account;
82
    }
83
}
84