Completed
Pull Request — master (#24)
by Quentin
03:30
created

LoaderTrait::getLoadingDelegate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
ccs 0
cts 6
cp 0
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 6
1
<?php
2
3
namespace Majora\Framework\Loader;
4
5
use Majora\Framework\Repository\RepositoryInterface;
6
use Symfony\Component\OptionsResolver\OptionsResolver;
7
8
/**
9
 * Base trait for loaders.
10
 *
11
 * @method retrieveById()
12
 */
13
trait LoaderTrait
14
{
15
    /**
16
     * @var RepositoryInterface
17
     */
18
    protected $entityRepository;
19
20
    /**
21
     * @var array
22
     */
23
    protected $entityProperties;
24
25
    /**
26
     * @var string
27
     */
28
    protected $entityClass;
29
30
    /**
31
     * @var string
32
     */
33
    protected $collectionClass;
34
35
    /**
36
     * @var OptionsResolver
37
     */
38
    protected $filterResolver;
39
40
    /**
41
     * @var \Closure
42
     */
43
    private $loadingDelegate;
44
45
    /**
46
     * setUp method.
47
     *
48
     * @param string              $entityClass
49
     * @param array               $entityProperties
50
     * @param string              $collectionClass
51
     * @param RepositoryInterface $entityRepository
52
     */
53
    public function setUp(
54
        $entityClass,
55
        array $entityProperties,
56
        $collectionClass,
57
        RepositoryInterface $entityRepository = null
58
    ) {
59
        @trigger_error(__METHOD__.'() is deprecated and will be removed in 2.0. Please use configureMetadata() instead.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
60
61
        if ($entityRepository) {
62
            @trigger_error('Repository injection throught '.__METHOD__.'() is deprecated and will be removed in 2.0. Please inject it by constructor.', E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
63
64
            $this->entityRepository = $entityRepository;
65
        }
66
67
        return $this->configureMetadata($entityClass, $entityProperties, $collectionClass);
68
    }
69
70
    /**
71
     * @see LoaderInterface::configureMetadata()
72
     */
73
    public function configureMetadata($entityClass, array $entityProperties, $collectionClass)
74
    {
75
        $this->entityClass = $entityClass;
76
        $this->entityProperties = $entityProperties;
77
        $this->collectionClass = $collectionClass;
78
79
        $this->filterResolver = new OptionsResolver();
80
        foreach ($entityProperties as $propertyName) {
81
            $this->filterResolver->setDefined($propertyName);
82
        }
83
    }
84
85
    /**
86
     * Returns repository loading delegate.
87
     *
88
     * @return \Closure
89
     */
90
    public function getLoadingDelegate()
91
    {
92
        return $this->loadingDelegate = $this->loadingDelegate ?: function ($id) {
93
            return $this->retrieveById($id);
0 ignored issues
show
Unused Code introduced by
The call to LoaderTrait::retrieveById() has too many arguments starting with $id.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
94
        };
95
    }
96
}
97