Passed
Push — main ( 49b4fd...94f20e )
by Marc
03:31
created

ModelFactory::getSummary()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
namespace html_go\model;
3
4
use DateTimeInterface;
5
use InvalidArgumentException;
6
use html_go\exceptions\InternalException;
7
use html_go\indexing\IndexManager;
8
use html_go\markdown\MarkdownParser;
9
10
/**
11
 * Responsible for creating <code>Content</code> objects ready to be used in templates.
12
 * @author Marc L. Veary
13
 * @since 1.0
14
 */
15
final class ModelFactory
16
{
17
    private Config $config;
18
    private MarkdownParser $parser;
19
    private IndexManager $manager;
20
21
    /**
22
     * ModelFactory constructor.
23
     * @param Config $config
24
     * @param MarkdownParser $parser Implementation of the
25
     * <code>MarkdownParser</code> interface.
26
     */
27
    public function __construct(Config $config, MarkdownParser $parser, IndexManager $manager) {
28
        $this->config = $config;
29
        $this->parser = $parser;
30
        $this->manager = $manager;
31
    }
32
33
    /**
34
     * Create a content object (stdClass) from an index object (stdClass).
35
     * @param \stdClass $indexElement As obtained from the <code>IndexManager</code>
36
     * @return \stdClass
37
     */
38
    public function createContentObject(\stdClass $indexElement): \stdClass {
39
        $contentObject = $this->getContentObject($indexElement);
40
        $contentObject->key = $indexElement->key;
41
        $contentObject->list = [];
42
        $contentObject->site = $this->getSiteObject();
43
44
        if (!empty($indexElement->category)) {
45
            $contentObject->category = $this->getCategoryObject($indexElement->category);
46
        }
47
        if (isset($indexElement->tags)) {
48
            $contentObject->tags = $indexElement->tags;
49
        }
50
        if (!empty($indexElement->date)) {
51
            $dt = $this->getContentDate($indexElement->date);
52
            $contentObject->date = $dt[0];
53
            $contentObject->timestamp = $dt[1];
54
        }
55
        if (empty($contentObject->summary)) {
56
            $contentObject->summary = $this->getSummary($contentObject->body);
57
        }
58
59
        return $contentObject;
60
    }
61
62
    private function getContentObject(\stdClass $indexElement): \stdClass {
63
        if ($indexElement->section === TAG_SECTION) {
64
            $contentObject = $this->createEmptyContentObject();
65
            $contentObject->title = \substr($indexElement->key, \strlen(TAG_SECTION) + 1);
66
        } else {
67
            $contentObject = $this->loadDataFile($indexElement);
68
            $contentObject->body = $this->restoreNewlines($contentObject->body);
69
        }
70
        return $contentObject;
71
    }
72
73
    /**
74
     * @param string $dateStr
75
     * @return array<string>
76
     */
77
    private function getContentDate(string $dateStr): array {
78
        if (EMPTY_VALUE === $dateStr) {
79
            $date = $timestamp = EMPTY_VALUE;
80
        } else {
81
            $dt = new \DateTime($dateStr);
82
            $date = $dt->format($this->config->getString(Config::KEY_POST_DATE_FMT));
83
            $timestamp = $dt->format(DateTimeInterface::W3C);
84
        }
85
        return [$date, $timestamp];
86
    }
87
88
    private function getCategoryObject(string $slug): \stdClass {
89
        if ($this->manager->elementExists($slug) === false) {
90
            throw new \UnexpectedValueException("Element does not exist [$slug]"); // @codeCoverageIgnore
91
        }
92
        return $this->loadDataFile($this->manager->getElementFromSlugIndex($slug));
93
    }
94
95
    private function getSiteObject(): \stdClass {
96
        static $site = null;
97
        if (empty($site)) {
98
            $site = new \stdClass();
99
            $site->url = $this->config->getString(Config::KEY_SITE_URL);
100
            $site->name = $this->config->getString(Config::KEY_SITE_NAME);
101
            $site->title = $this->config->getString(Config::KEY_SITE_TITLE);
102
            $site->description = $this->config->getString(Config::KEY_SITE_DESCRIPTION);
103
            $site->tagline = $this->config->getString(Config::KEY_SITE_TAGLINE);
104
            $site->copyright = $this->config->getString(Config::KEY_SITE_COPYRIGHT);
105
            $site->language = $this->config->getString(Config::KEY_LANG);
106
            $site->theme = $this->config->getString(Config::KEY_THEME_NAME);
107
            $site->tpl_engine = $this->config->getString(Config::KEY_TPL_ENGINE);
108
        }
109
        return $site;
110
    }
111
112
    private function loadDataFile(\stdClass $indexElement): \stdClass {
113
        if (empty($indexElement->path)) {
114
            throw new InvalidArgumentException("Object does not have 'path' property "./** @scrutinizer ignore-type */print_r($indexElement, true)); // @codeCoverageIgnore
115
        }
116
        $path = $indexElement->path;
117
        if (($data = \file_get_contents($path)) === false) {
118
            throw new InternalException("file_get_contents() failed opening [$path]"); // @codeCoverageIgnore
119
        }
120
        if (($contentObject = \json_decode($data)) === null) {
121
            throw new InternalException("json_decode returned null decoding [$data] from [$path]"); // @codeCoverageIgnore
122
        }
123
        return $contentObject;
124
    }
125
126
    /**
127
     * This is used for tags only as tags don't have an associated file on the
128
     * filesystem.
129
     * @return \stdClass
130
     */
131
    private function createEmptyContentObject(): \stdClass {
132
        $obj = new \stdClass();
133
        $obj->key = EMPTY_VALUE;
134
        $obj->section = EMPTY_VALUE;
135
        $obj->body = EMPTY_VALUE;
136
        $obj->title = EMPTY_VALUE;
137
        $obj->description = EMPTY_VALUE;
138
        return $obj;
139
    }
140
141
    /**
142
     * Returns the summary for the content object. If '<!--more-->' is used within
143
     * the body, then this is removed once the summary is obtained.
144
     * @param string $body the body. The '<!--more--> removed if found thus passed by reference.
145
     * @return string The summary text. If no summary is defined, returns an empty string
146
     */
147
    private function getSummary(string &$body): string {
148
        $pos = \strpos($body, '<!--more-->');
149
        if ($pos !== false) {
150
            $summary = \substr($body, 0, $pos);
151
            $body = \str_replace('<!--more-->', '', $body);
152
            return $summary;
153
        }
154
        return '';
155
    }
156
157
    /**
158
     * Newlines must be encoded for PHP functions. So we use '<nl>' to for '\n'.
159
     * This method replaces '<nl>' with '\n'.
160
     * @param string $text
161
     * @return string
162
     */
163
    private function restoreNewlines(string $text): string {
164
        return \str_replace('<nl>', '\n', $text);
165
    }
166
}
167