Passed
Push — master ( c12552...22ad80 )
by Gabor
04:56
created

GetCategoriesHelper::__invoke()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 38
Code Lines 20

Duplication

Lines 38
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 38
loc 38
ccs 0
cts 25
cp 0
rs 8.439
c 0
b 0
f 0
cc 5
eloc 20
nc 3
nop 0
crap 30
1
<?php
2
/**
3
 * WebHemi.
4
 *
5
 * PHP version 7.1
6
 *
7
 * @copyright 2012 - 2017 Gixx-web (http://www.gixx-web.com)
8
 * @license   https://opensource.org/licenses/MIT The MIT License (MIT)
9
 *
10
 * @link      http://www.gixx-web.com
11
 */
12
declare(strict_types = 1);
13
14
namespace WebHemi\Renderer\Helper;
15
16
use WebHemi\Data\StorageInterface;
17
use WebHemi\Data\Storage;
18
use WebHemi\Data\Entity;
19
use WebHemi\Environment\ServiceInterface as EnvironmentInterface;
20
use WebHemi\Renderer\HelperInterface;
21
use WebHemi\Router\ProxyInterface;
22
use WebHemi\StorageTrait;
23
24
/**
25
 * Class GetCategoriesHelper
26
 */
27 View Code Duplication
class GetCategoriesHelper implements HelperInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
{
29
    /** @var EnvironmentInterface */
30
    private $environmentManager;
31
32
    use StorageTrait;
33
34
    /**
35
     * GetCategoriesHelper constructor.
36
     *
37
     * @param EnvironmentInterface $environmentManager
38
     * @param StorageInterface[] ...$dataStorages
39
     */
40
    public function __construct(EnvironmentInterface $environmentManager, StorageInterface ...$dataStorages)
41
    {
42
        $this->environmentManager = $environmentManager;
43
        $this->addStorageInstances($dataStorages);
0 ignored issues
show
Documentation introduced by
$dataStorages is of type array<integer,array<inte...ata\StorageInterface>>>, but the function expects a array<integer,object<Web...Data\StorageInterface>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
44
    }
45
46
    /**
47
     * Should return the name of the helper.
48
     *
49
     * @return string
50
     */
51
    public static function getName() : string
52
    {
53
        return 'getCategories';
54
    }
55
56
    /**
57
     * Should return the name of the helper.
58
     *
59
     * @return string
60
     */
61
    public static function getDefinition() : string
62
    {
63
        return '{{ getCategories(order_by, limit) }}';
64
    }
65
66
    /**
67
     * Gets helper options for the render.
68
     *
69
     * @return array
70
     * @codeCoverageIgnore - empty array
71
     */
72
    public static function getOptions() : array
73
    {
74
        return [];
75
    }
76
77
    /**
78
     * Should return a description text.
79
     *
80
     * @return string
81
     */
82
    public static function getDescription() : string
83
    {
84
        return 'Returns the categories for the current application.';
85
    }
86
87
    /**
88
     * A renderer helper should be called with its name.
89
     *
90
     * @return array
91
     */
92
    public function __invoke() : array
93
    {
94
        $categories = [];
95
96
        /** @var Storage\ApplicationStorage $applicationStorage */
97
        $applicationStorage = $this->getApplicationStorage();
98
        /** @var Storage\Filesystem\FilesystemCategoryStorage $categoryStorage */
99
        $categoryStorage = $this->getFilesystemCategoryStorage();
100
        /** @var Storage\Filesystem\FilesystemDirectoryStorage $directoryStorage */
101
        $directoryStorage = $this->getFilesystemDirectoryStorage();
102
103
        if (!$applicationStorage || !$categoryStorage || !$directoryStorage) {
104
            return [];
105
        }
106
107
        /** @var Entity\ApplicationEntity $application */
108
        $application = $applicationStorage
109
            ->getApplicationByName($this->environmentManager->getSelectedApplication());
110
        $applicationId = $application->getKeyData();
111
112
        /** @var array $categoryDirectoryData */
113
        $categoryDirectoryData = $directoryStorage
114
            ->getDirectoryDataByApplicationAndProxy($applicationId, ProxyInterface::LIST_CATEGORY);
115
116
        /** @var Entity\Filesystem\FilesystemCategoryEntity[] $categoryList */
117
        $categoryList = $categoryStorage
118
            ->getFilesystemCategorysByApplication($applicationId);
119
120
        foreach ($categoryList as $categoryEntity) {
121
            $categories[] = [
122
                'path' => $categoryDirectoryData['uri'],
123
                'name' => $categoryEntity->getName(),
124
                'title' => $categoryEntity->getTitle()
125
            ];
126
        }
127
128
        return $categories;
129
    }
130
}
131