MysqlRepository   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 56
rs 10
c 0
b 0
f 0
wmc 6

5 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 3 1
A __construct() 0 4 1
A oneOrException() 0 10 2
A register() 0 4 1
A setRepository() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Infrastructure\Common\Query;
6
7
use Doctrine\ORM\EntityManagerInterface;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Doctrine\ORM\EntityRepository;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\EntityRepository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Doctrine\ORM\QueryBuilder;
0 ignored issues
show
Bug introduced by
The type Doctrine\ORM\QueryBuilder was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
0 ignored issues
show
Bug introduced by
The type Symfony\Component\HttpKe...n\NotFoundHttpException was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
12
/**
13
 * Class MysqlRepository.
14
 */
15
abstract class MysqlRepository
16
{
17
    /**
18
     * @param object $model
19
     */
20
    public function register(object $model): void
21
    {
22
        $this->entityManager->persist($model);
23
        $this->apply();
24
    }
25
26
    public function apply(): void
27
    {
28
        $this->entityManager->flush();
29
    }
30
31
    /**
32
     * @throws NotFoundHttpException
33
     * @throws \Doctrine\ORM\NonUniqueResultException
34
     */
35
    protected function oneOrException(QueryBuilder $queryBuilder)
36
    {
37
        $model = $queryBuilder
38
            ->getQuery()
39
            ->getOneOrNullResult();
40
        if (null === $model) {
41
            throw new NotFoundHttpException();
42
        }
43
44
        return $model;
45
    }
46
47
    private function setRepository(string $model): void
48
    {
49
        /** @var EntityRepository $objectRepository */
50
        $objectRepository = $this->entityManager->getRepository($model);
51
        $this->repository = $objectRepository;
52
    }
53
54
    /**
55
     * MysqlRepository constructor.
56
     */
57
    public function __construct(EntityManagerInterface $entityManager)
58
    {
59
        $this->entityManager = $entityManager;
60
        $this->setRepository($this->class);
61
    }
62
63
    /** @var string */
64
    protected $class;
65
    /** @var EntityRepository */
66
    protected $repository;
67
    /**
68
     * @var EntityManagerInterface
69
     */
70
    protected $entityManager;
71
}
72