Test Failed
Push — master ( dc15ab...6abe5f )
by Szymon
01:26
created

MysqlRepository::setRepository()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 10
c 0
b 0
f 0
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 $model
19
     */
20
    public function get($model, string $id): object
21
    {
22
        $entity = $this->entityManager->getRepository($model)->find($id);
23
24
        return $entity;
25
    }
26
27
    /**
28
     * @param $model
29
     */
30
    public function register($model): void
31
    {
32
        $this->entityManager->persist($model);
33
        $this->apply();
34
    }
35
36
    public function apply(): void
37
    {
38
        $this->entityManager->flush();
39
    }
40
41
    /**
42
     * @throws NotFoundHttpException
43
     * @throws \Doctrine\ORM\NonUniqueResultException
44
     */
45
    protected function oneOrException(QueryBuilder $queryBuilder)
46
    {
47
        $model = $queryBuilder
48
            ->getQuery()
49
            ->getOneOrNullResult();
50
        if (null === $model) {
51
            throw new NotFoundHttpException();
52
        }
53
54
        return $model;
55
    }
56
57
    private function setRepository(string $model): void
58
    {
59
        /** @var EntityRepository $objectRepository */
60
        $objectRepository = $this->entityManager->getRepository($model);
61
        $this->repository = $objectRepository;
62
    }
63
64
    /**
65
     * MysqlRepository constructor.
66
     */
67
    public function __construct(EntityManagerInterface $entityManager)
68
    {
69
        $this->entityManager = $entityManager;
70
        $this->setRepository($this->class);
71
    }
72
73
    /** @var string */
74
    protected $class;
75
    /** @var EntityRepository */
76
    protected $repository;
77
    /**
78
     * @var EntityManagerInterface
79
     */
80
    protected $entityManager;
81
}
82