Failed Conditions
Push — master ( 7c4d30...258d19 )
by Adrien
07:17
created

AccountRepository::getOrCreate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 3.0021

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 1
dl 0
loc 22
ccs 15
cts 16
cp 0.9375
crap 3.0021
rs 9.7666
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
12
{
13
    private const PARENT_ACCOUNT_ID_FOR_USER = 10011;
14
15
    /**
16
     * This will return, and potentially create, an account for the given user
17
     *
18
     * @param User $user
19
     *
20
     * @return Account
21
     */
22 3
    public function getOrCreate(User $user): Account
23
    {
24 3
        $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

24
        /** @scrutinizer ignore-call */ 
25
        $account = $this->findOneByOwner($user);
Loading history...
25 3
        if (!$account) {
26 2
            $account = new Account();
27 2
            $this->getEntityManager()->persist($account);
28 2
            $account->setOwner($user);
29 2
            $account->setType(AccountTypeType::LIABILITY);
30 2
            $account->setName($user->getName());
31
32 2
            $maxCode = $this->getEntityManager()->getConnection()->fetchColumn('SELECT MAX(code) FROM account WHERE parent_id = ' . self::PARENT_ACCOUNT_ID_FOR_USER);
33 2
            $newCode = ++$maxCode;
34 2
            $account->setCode((string) $newCode);
35
36 2
            $parent = $this->findOneById(self::PARENT_ACCOUNT_ID_FOR_USER);
37 2
            if (!$parent) {
38
                throw new \Exception('Cannot find parent account for creation of user account');
39
            }
40 2
            $account->setParent($parent);
41
        }
42
43 3
        return $account;
44
    }
45
}
46