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

ArchiveAction::getTemplateData()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 51
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 51
rs 8.6588
c 0
b 0
f 0
cc 6
eloc 27
nc 5
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\DateTime;
18
use WebHemi\Data\Entity;
19
use WebHemi\Middleware\Action\Website\IndexAction;
20
21
/**
22
 * Class ArchiveAction.
23
 */
24
class ArchiveAction extends IndexAction
25
{
26
    /**
27
     * Gets template map name or template file path.
28
     *
29
     * @return string
30
     */
31
    public function getTemplateName() : string
32
    {
33
        return 'website-post-list';
34
    }
35
36
    /**
37
     * Gets template data.
38
     *
39
     * @return array
40
     */
41
    public function getTemplateData() : array
42
    {
43
        $blogPosts = [];
44
        $parameters = $this->getRoutingParameters();
45
        $date = $parameters['uri_parameter'] ?? null;
46
47
        if (!$date) {
48
            throw new RuntimeException('Forbidden', 403);
49
        }
50
51
        $dateParts = explode('-', $date);
52
53
        if (!preg_match('/^\d{4}\-\d{2}$/', $date) || !checkdate((int) ($dateParts[1] ?? 13), 1, (int) $dateParts[0])) {
54
            throw new RuntimeException('Bad Request', 400);
55
        }
56
57
        /** @var Entity\ApplicationEntity $applicationEntity */
58
        $applicationEntity = $this->getApplicationStorage()
59
            ->getApplicationByName($this->environmentManager->getSelectedApplication());
60
61
        /** @var Entity\Filesystem\FilesystemEntity[] $publications */
62
        $publications = $this->getFilesystemStorage()
63
            ->getPublishedDocuments(
64
                $applicationEntity->getApplicationId(),
65
                [
66
                    'YEAR(date_published) = ?' => (int)$dateParts[0],
67
                    'MONTH(date_published) = ?' => (int)$dateParts[1]
68
                ]
69
            );
70
71
        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...
72
            throw new RuntimeException('Not Found', 404);
73
        }
74
75
        /** @var DateTime $titleDate */
76
        $titleDate = $publications[0]->getDatePublished();
77
78
        /** @var Entity\Filesystem\FilesystemEntity $filesystemEntity */
79
        foreach ($publications as $filesystemEntity) {
80
            $blogPosts[] = $this->getBlobPostData($applicationEntity, $filesystemEntity);
81
        }
82
83
        return [
84
            'page' => [
85
                'title' => $titleDate->format('Y4B'),
86
                'type' => 'Archive',
87
            ],
88
            'activeMenu' => $date,
89
            'blogPosts' => $blogPosts,
90
        ];
91
    }
92
}
93