Passed
Branch master (0828fa)
by Gabor
03:19
created

CategoryAction::getTemplateData()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 52
Code Lines 28

Duplication

Lines 52
Ratio 100 %

Importance

Changes 0
Metric Value
dl 52
loc 52
rs 8.6868
c 0
b 0
f 0
cc 5
eloc 28
nc 6
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\Middleware\Action\Website\Directory;
15
16
use RuntimeException;
17
use WebHemi\Data\Entity;
18
use WebHemi\Middleware\Action\Website\IndexAction;
19
20
/**
21
 * Class CategoryAction.
22
 */
23 View Code Duplication
class CategoryAction extends IndexAction
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...
24
{
25
    /** @var string */
26
    protected $templateName = 'website-post-list';
27
28
    /**
29
     * Gets template map name or template file path.
30
     *
31
     * @return string
32
     */
33
    public function getTemplateName() : string
34
    {
35
        return $this->templateName;
36
    }
37
38
    /**
39
     * Gets template data.
40
     *
41
     * @return array
42
     */
43
    public function getTemplateData() : array
44
    {
45
        $blogPosts = [];
46
        $parameters = $this->getRoutingParameters();
47
        /** @var string $category */
48
        $category = $parameters['uri_parameter'] ?? null;
49
50
        if (!$category) {
51
            throw new RuntimeException('Forbidden', 403);
52
        }
53
54
        /** @var Entity\ApplicationEntity $applicationEntity */
55
        $applicationEntity = $this->getApplicationStorage()
56
            ->getApplicationByName($this->environmentManager->getSelectedApplication());
57
58
        $categoryEntity = $this->getFilesystemCategoryStorage()
59
            ->getFilesystemCategoryByApplicationAndName(
60
                $applicationEntity->getApplicationId(),
61
                $category
62
            );
63
64
        if (!$categoryEntity) {
65
            throw new RuntimeException('Not Found', 404);
66
        }
67
68
        /** @var Entity\Filesystem\FilesystemEntity[] $publications */
69
        $publications = $this->getFilesystemStorage()
70
            ->getPublishedDocuments(
71
                $applicationEntity->getApplicationId(),
72
                [
73
                    'fk_category = ?' => (int)$categoryEntity->getFilesystemCategoryId(),
74
                ]
75
            );
76
77
        if (!$publications) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $publications of type WebHemi\Data\Entity\Filesystem\FilesystemEntity[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
78
            $this->templateName = 'website-post-list-empty';
79
        }
80
81
        /** @var Entity\Filesystem\FilesystemEntity $filesystemEntity */
82
        foreach ($publications as $filesystemEntity) {
83
            $blogPosts[] = $this->getBlobPostData($applicationEntity, $filesystemEntity);
84
        }
85
86
        return [
87
            'page' => [
88
                'title' => $categoryEntity->getTitle(),
89
                'type' => 'Categories',
90
            ],
91
            'activeMenu' => $category,
92
            'blogPosts' => $blogPosts,
93
        ];
94
    }
95
}
96