Passed
Push — main ( 43b021...6333de )
by Marc
03:25
created

ModelFactory::getSiteObject()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 13
nc 2
nop 0
dl 0
loc 15
rs 9.8333
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->loadDataFile($indexElement);
40
        $contentObject->body = $this->restoreNewlines($contentObject->body);
41
        $contentObject->key = $indexElement->key;
42
        if (!empty($indexElement->category)) {
43
            $contentObject->category = $this->getCategoryObject($indexElement->category);
44
        }
45
        if (isset($indexElement->tags)) {
46
            $contentObject->tags = $indexElement->tags;
47
        }
48
        if (!empty($indexElement->date)) {
49
            $dt = $this->getContentDate($indexElement->date);
50
            $contentObject->date = $dt[0];
51
            $contentObject->timestamp = $dt[1];
52
        }
53
        if (empty($contentObject->summary)) {
54
            $contentObject->summary = $this->getSummary($contentObject->body);
55
        }
56
        $contentObject->listing = [];
57
        $contentObject->site = $this->getSiteObject();
58
        return $contentObject;
59
    }
60
61
    /**
62
     * @param string $dateStr
63
     * @return array<string>
64
     */
65
    private function getContentDate(string $dateStr): array {
66
        if (EMPTY_VALUE === $dateStr) {
67
            $date = $timestamp = EMPTY_VALUE;
68
        } else {
69
            $dt = new \DateTime($dateStr);
70
            $date = $dt->format($this->config->getString(Config::KEY_POST_DATE_FMT));
71
            $timestamp = $dt->format(DateTimeInterface::W3C);
72
        }
73
        return [$date, $timestamp];
74
    }
75
76
    private function getCategoryObject(string $slug): \stdClass {
77
        if ($this->manager->elementExists($slug) === false) {
78
            throw new \UnexpectedValueException("Element does not exist [$slug]");
79
        }
80
        return $this->loadDataFile($this->manager->getElementFromSlugIndex($slug));
81
    }
82
83
    private function getSiteObject(): \stdClass {
84
        static $site = null;
85
        if (empty($site)) {
86
            $site = new \stdClass();
87
            $site->url = $this->config->getString(Config::KEY_SITE_URL);
88
            $site->name = $this->config->getString(Config::KEY_SITE_NAME);
89
            $site->title = $this->config->getString(Config::KEY_SITE_TITLE);
90
            $site->description = $this->config->getString(Config::KEY_SITE_DESCRIPTION);
91
            $site->tagline = $this->config->getString(Config::KEY_SITE_TAGLINE);
92
            $site->copyright = $this->config->getString(Config::KEY_SITE_COPYRIGHT);
93
            $site->language = $this->config->getString(Config::KEY_LANG);
94
            $site->theme = $this->config->getString(Config::KEY_THEME_NAME);
95
            $site->tpl_engine = $this->config->getString(Config::KEY_TPL_ENGINE);
96
        }
97
        return $site;
98
    }
99
100
    private function loadDataFile(\stdClass $indexElement): \stdClass {
101
        if (!isset($indexElement->path) || empty($indexElement->path)) {
102
            throw new InvalidArgumentException("Object does not have 'path' property "./** @scrutinizer ignore-type */print_r($indexElement, true)); // @codeCoverageIgnore
103
        }
104
        if (($data = \file_get_contents($indexElement->path)) === false) {
105
            throw new InternalException("file_get_contents() failed opening [$indexElement->path]"); // @codeCoverageIgnore
106
        }
107
        if (($contentObject = \json_decode($data)) === null) {
108
            $path = $indexElement->path;
109
            throw new InternalException("json_decode returned null decoding [$data] from [$path]"); // @codeCoverageIgnore
110
        }
111
        return $contentObject;
112
    }
113
114
    /**
115
     * Returns the summary for the content object. If '<!--more-->' is used within
116
     * the body, then this is removed once the summary obtained.
117
     * @param string $body the body with the '<!--more--> removed
118
     * @return string The summary text. If no summary is defined, returns an empty string
119
     */
120
    private function getSummary(string &$body): string {
121
        $pos = \strpos($body, '<!--more-->');
122
        if ($pos !== false) {
123
            $summary = \substr($body, 0, $pos);
124
            $body = \str_replace('<!--more-->', '', $body);
125
            return $summary;
126
        }
127
        return '';
128
    }
129
130
    /**
131
     * Newlines must be encoded for PHP functions. So we use '<nl>' to for '\n'.
132
     * This method replaces '<nl>' with '\n'.
133
     * @param string $text
134
     * @return string
135
     */
136
    private function restoreNewlines(string $text): string {
137
        return \str_replace('<nl>', '\n', $text);
138
    }
139
}
140