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

FilesystemDocumentStorage   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 131
ccs 0
cts 60
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A populateEntity() 0 12 3
A getEntityData() 0 17 3
A getFilesystemDocumentById() 0 7 1
B getFilesystemDocuments() 0 39 5
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\Data\Storage\Filesystem;
15
16
use WebHemi\DateTime;
17
use WebHemi\Data\ConnectorInterface;
18
use WebHemi\Data\EntityInterface;
19
use WebHemi\Data\Storage\AbstractStorage;
20
use WebHemi\Data\Entity\Filesystem\FilesystemDocumentEntity;
21
22
/**
23
 * Class FilesystemDocumentStorage.
24
 */
25
class FilesystemDocumentStorage extends AbstractStorage
26
{
27
    /** @var string */
28
    protected $dataGroup = 'webhemi_filesystem_document';
29
    /** @var string */
30
    protected $idKey = 'id_filesystem_document';
31
    /** @var string */
32
    protected $idParentRevision = 'fk_parent_revision';
33
    /** @var string */
34
    protected $idAuthor = 'fk_author';
35
    /** @var string */
36
    protected $contentRevision = 'content_revision';
37
    /** @var string */
38
    protected $contentLead = 'content_lead';
39
    /** @var string */
40
    protected $contentBody = 'content_body';
41
    /** @var string */
42
    private $dateCreated = 'date_created';
43
    /** @var string */
44
    private $dateModified = 'date_modified';
45
46
    /**
47
     * Populates an entity with storage data.
48
     *
49
     * @param EntityInterface $dataEntity
50
     * @param array           $data
51
     * @return void
52
     */
53
    protected function populateEntity(EntityInterface&$dataEntity, array $data) : void
54
    {
55
        /* @var FilesystemDocumentEntity $dataEntity */
56
        $dataEntity->setFilesystemDocumentId((int) $data[$this->idKey])
57
            ->setParentRevisionId(isset($data[$this->idParentRevision]) ? (int) $data[$this->idParentRevision] : null)
58
            ->setAuthorId(isset($data[$this->idAuthor]) ? (int) $data[$this->idAuthor] : null)
59
            ->setContentRevision((int) ($data[$this->contentRevision] ?? 1))
60
            ->setContentLead($data[$this->contentLead])
61
            ->setContentBody($data[$this->contentBody])
62
            ->setDateCreated(new DateTime($data[$this->dateCreated] ?? 'now'))
63
            ->setDateModified(new DateTime($data[$this->dateModified] ?? 'now'));
64
    }
65
66
    /**
67
     * Get data from an entity.
68
     *
69
     * @param EntityInterface $dataEntity
70
     * @return array
71
     */
72
    protected function getEntityData(EntityInterface $dataEntity) : array
73
    {
74
        /** @var FilesystemDocumentEntity $dataEntity */
75
        $dateCreated = $dataEntity->getDateCreated();
76
        $dateModified = $dataEntity->getDateModified();
77
78
        return [
79
            $this->idKey => (int) $dataEntity->getKeyData(),
80
            $this->idParentRevision => $dataEntity->getParentRevisionId(),
81
            $this->idAuthor => $dataEntity->getAuthorId(),
82
            $this->contentRevision => (int) $dataEntity->getContentRevision(),
83
            $this->contentLead => $dataEntity->getContentLead(),
84
            $this->contentBody => $dataEntity->getContentBody(),
85
            $this->dateCreated => $dateCreated instanceof DateTime ? $dateCreated->format('Y-m-d H:i:s') : null,
86
            $this->dateModified => $dateModified instanceof DateTime ? $dateModified->format('Y-m-d H:i:s') : null
87
        ];
88
    }
89
90
    /**
91
     * Gets the filesystem document entity by the identifier.
92
     *
93
     * @param int $identifier
94
     * @return null|FilesystemDocumentEntity
95
     */
96
    public function getFilesystemDocumentById(int $identifier) : ? FilesystemDocumentEntity
97
    {
98
        /** @var null|FilesystemDocumentEntity $dataEntity */
99
        $dataEntity = $this->getDataEntity([$this->idKey => $identifier]);
100
101
        return $dataEntity;
102
    }
103
104
    /**
105
     * Gets the published documents
106
     *
107
     * @param array $filesystemDocumentIds
108
     * @param array $additionalExpressions
109
     * @param string|null $order
110
     * @param int|null $limit
111
     * @param int|null $offset
112
     * @param string|null $groupBy
113
     * @param string|null $having
114
     * @return FilesystemDocumentEntity[]
115
     */
116
    public function getFilesystemDocuments(
117
        array $filesystemDocumentIds,
118
        array $additionalExpressions = [],
119
        string $order = null,
120
        int $limit = null,
121
        int $offset = null,
122
        string $groupBy = null,
123
        string $having = null
124
    ) : ? array {
125
        $defaultExpressions = [$this->idKey => $filesystemDocumentIds];
126
127
        // This way the default ones can be overwritten.
128
        $expressions = array_merge($defaultExpressions, $additionalExpressions);
129
130
        $options = [
131
            ConnectorInterface::OPTION_ORDER => ($order ?? $this->dateCreated.' ASC')
132
        ];
133
134
        if (is_numeric($limit)) {
135
            $options[ConnectorInterface::OPTION_LIMIT] = (int) $limit;
136
137
            if (is_numeric($offset)) {
138
                $options[ConnectorInterface::OPTION_OFFSET] = (int) $offset;
139
            }
140
        }
141
142
        if (!empty($groupBy)) {
143
            $options[ConnectorInterface::OPTION_GROUP] = $groupBy;
144
145
            if (!empty($having)) {
146
                $options[ConnectorInterface::OPTION_HAVING] = $having;
147
            }
148
        }
149
150
        /** @var FilesystemDocumentEntity[] $dataEntitySet */
151
        $dataEntitySet = $this->getDataEntitySet($expressions, $options);
152
153
        return $dataEntitySet;
154
    }
155
}
156