Completed
Push — master ( 84cc26...61df42 )
by Adrien
08:55
created

AccountRepository::getOrCreate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 3.0015

Importance

Changes 0
Metric Value
cc 3
eloc 17
nc 3
nop 1
dl 0
loc 25
ccs 17
cts 18
cp 0.9444
crap 3.0015
rs 9.7
c 0
b 0
f 0
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 11
    public function getAccessibleSubQuery(?User $user): string
23
    {
24 11
        if (!$user) {
25 1
            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 $this->getAllIdsForOwnerQuery($user);
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 4
    private function getOneById(int $id): ?Account
45
    {
46 4
        $this->getAclFilter()->setEnabled(false);
47 4
        $account = $this->findOneById($id);
48 4
        $this->getAclFilter()->setEnabled(true);
49
50 4
        return $account;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $account could return the type Application\Model\AbstractModel which includes types incompatible with the type-hinted return Application\Model\Account|null. Consider adding an additional type-check to rule them out.
Loading history...
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 8
    public function getOrCreate(User $user): Account
61
    {
62 8
        $this->getAclFilter()->setEnabled(false);
63 8
        $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

63
        /** @scrutinizer ignore-call */ 
64
        $account = $this->findOneByOwner($user);
Loading history...
64 8
        $this->getAclFilter()->setEnabled(true);
65
66 8
        if (!$account) {
67 4
            $account = new Account();
68 4
            $this->getEntityManager()->persist($account);
69 4
            $account->setOwner($user);
70 4
            $account->setType(AccountTypeType::LIABILITY);
71 4
            $account->setName($user->getName());
72
73 4
            $maxCode = $this->getEntityManager()->getConnection()->fetchColumn('SELECT MAX(code) FROM account WHERE parent_id = ' . self::PARENT_ACCOUNT_ID_FOR_USER);
74 4
            $newCode = ++$maxCode;
75 4
            $account->setCode((string) $newCode);
76
77 4
            $parent = $this->getOneById(self::PARENT_ACCOUNT_ID_FOR_USER);
78 4
            if (!$parent) {
79
                throw new \Exception('Cannot find parent account for creation of user account');
80
            }
81 4
            $account->setParent($parent);
82
        }
83
84 8
        return $account;
85
    }
86
87
    /**
88
     * Sum balance by account type
89
     *
90
     * @API\Input(type="AccountType")
91
     *
92
     * @param string $accountType
93
     *
94
     * @return string
95
     */
96 1
    public function totalBalanceByType(string $accountType): string
97
    {
98 1
        $qb = $this->getEntityManager()->getConnection()->createQueryBuilder()
99 1
            ->select('SUM(balance)')
100 1
            ->from($this->getClassMetadata()->getTableName())
101 1
            ->where('type = :type');
102
103 1
        $qb->setParameter('type', $accountType);
104
105 1
        $result = $qb->execute();
106
107 1
        return (string) $result->fetchColumn();
108
    }
109
}
110