Test Failed
Push — master ( 7bbbf4...34ff55 )
by Julien
04:57
created

LifeCycle::getLifeCycleQuery()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 8
ccs 0
cts 5
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Model;
13
14
use Phalcon\Db\Column;
15
use Phalcon\Di;
16
use Phalcon\Mvc\Model\ManagerInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Zemit\Mvc\Model\ManagerInterface. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
17
use Phalcon\Mvc\Model\Query\Builder;
18
use Phalcon\Mvc\Model\QueryInterface;
19
use Phalcon\Mvc\Model\ResultsetInterface;
20
use Zemit\Mvc\Model;
21
use Zemit\Mvc\Model\AbstractTrait\AbstractEventsManager;
22
use Zemit\Mvc\Model\AbstractTrait\AbstractModelsManager;
23
24
trait LifeCycle
25
{
26
    use AbstractModelsManager;
27
    
28
    /**
29
     * Return the query for data retention
30
     */
31
    public static function prepareLifeCycleQuery(Builder $builder = null, ?array $parameters = null): void
32
    {
33
        // data life cycle policy must be defined
34
        if (empty($parameters)) {
35
            $builder->where('false');
0 ignored issues
show
Bug introduced by
The method where() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

35
            $builder->/** @scrutinizer ignore-call */ 
36
                      where('false');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
36
            $builder->setBindParams([]);
37
            $builder->setBindTypes([]);
38
        }
39
    }
40
    
41
    public static function getLifeCyclePolicy(): array
42
    {
43
        $config = Di::getDefault()->get('config');
44
        $dataLifeCycleConfig = $config->pathToArray('dataLifeCycle');
45
        $policyName = $dataLifeCycleConfig['models'][self::class] ?? null;
46
        return $dataLifeCycleConfig['models'][$policyName] ?? [];
47
    }
48
    
49
    public static function getLifeCyclePolicyQuery(): ?array
50
    {
51
        return self::getLifeCyclePolicy()['query'] ?? null;
52
    }
53
    
54
    /**
55
     * Return the Query for data retention
56
     */
57
    public static function getLifeCycleQuery(?array $parameters = null, ?Builder $builder = null): QueryInterface
58
    {
59
        $parameters ??= self::getLifeCyclePolicyQuery();
60
        $builder ??= self::getBuilder($parameters);
61
        
62
        self::prepareLifeCycleQuery($builder, $parameters);
63
        
64
        return $builder->getQuery();
65
    }
66
    
67
    /**
68
     * Return a Query Builder based on parameters
69
     */
70
    public static function getBuilder(?array $parameters = null): Builder
71
    {
72
        $di = Di::getDefault();
73
        $modelsManager = $di->getShared('modelsManager');
74
        assert($modelsManager instanceof ManagerInterface);
75
        
76
        $builder = $modelsManager->createBuilder($parameters);
0 ignored issues
show
Bug introduced by
It seems like $parameters can also be of type array; however, parameter $params of Phalcon\Mvc\Model\Manage...erface::createBuilder() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

76
        $builder = $modelsManager->createBuilder(/** @scrutinizer ignore-type */ $parameters);
Loading history...
77
        $builder->from(get_called_class());
78
        
79
        if (isset($parameters['limit'])) {
80
            $builder->limit($parameters['limit']);
81
        }
82
        
83
        return $builder;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $builder returns the type Phalcon\Mvc\Model\Query\BuilderInterface which includes types incompatible with the type-hinted return Phalcon\Mvc\Model\Query\Builder.
Loading history...
84
    }
85
    
86
    /**
87
     * Find records to hard delete for data retention purpose
88
     * @return mixed
89
     */
90
    public static function findLifeCycle(?array $parameters = null)
91
    {
92
        $query = self::getLifeCycleQuery($parameters);
93
        $resultset = $query->execute();
94
        
95
        if ($resultset instanceof ResultsetInterface) {
96
            if (isset($parameters['hydration'])) {
97
                $resultset->setHydrateMode($parameters['hydration']);
98
            }
99
        }
100
        
101
        return $resultset;
102
    }
103
}
104