Completed
Push — master ( 48c27a...1c124a )
by Joschi
03:20
created

resetRefersToRelations()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
cc 2
eloc 4
nc 2
nop 0
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 9.6666
1
<?php
2
3
/**
4
 * apparat-object
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Object
8
 * @subpackage  Apparat\Object\Infrastructure
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation files (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Object\Infrastructure\Utilities;
38
39
use Apparat\Kernel\Ports\Kernel;
40
use Apparat\Object\Application\Contract\CommonMarkPayloadProcessorInterface;
41
use Apparat\Object\Application\Model\Object\AbstractCommonMarkObject;
42
use Apparat\Object\Ports\Relation;
43
use League\CommonMark\Block\Element\Document;
44
use League\CommonMark\DocParser;
45
use League\CommonMark\DocumentProcessorInterface;
46
use League\CommonMark\Environment;
47
use League\CommonMark\Inline\Element\Image;
48
use League\CommonMark\Inline\Element\Link;
49
50
/**
51
 * CommonMark payload processor
52
 *
53
 * @package Apparat\Object
54
 * @subpackage Apparat\Object\Infrastructure
55
 */
56
class CommonMarkPayloadProcessor implements CommonMarkPayloadProcessorInterface, DocumentProcessorInterface
57
{
58
    /**
59
     * Mailto URL scheme
60
     *
61
     * @var string
62
     */
63
    const SCHEME_MAILTO = 'mailto';
64
    /**
65
     * Owning CommonMark object
66
     *
67
     * @var AbstractCommonMarkObject
68
     */
69
    protected $object;
70
    /**
71
     * List of refers-to relation URLs
72
     *
73
     * @var array
74
     */
75
    protected $refersTo;
76
    /**
77
     * List of embeds relation URLs
78
     *
79
     * @var array
80
     */
81
    protected $embeds;
82
83
    /**
84
     * Associate the processor with the owning CommonMark object
85
     *
86
     * @param AbstractCommonMarkObject $object CommonMark object
87
     */
88 22
    public function setObject(AbstractCommonMarkObject $object)
89
    {
90 22
        $this->object = $object;
91 22
    }
92
93
    /**
94
     * Process the payload of a CommonMark object
95
     *
96
     * @return AbstractCommonMarkObject CommonMark object
97
     */
98 2
    public function processPayload()
99
    {
100
        // Reset all relevant relations
101 2
        $this->resetRefersToRelations();
102 2
        $this->resetEmbedsRelations();
103
104 2
        $env = Environment::createCommonMarkEnvironment();
105 2
        $env->addDocumentProcessor($this);
106
107
        // Parse and process the object payload
108
        /** @var DocParser $docParser */
109 2
        $docParser = Kernel::create(DocParser::class, [$env]);
110 2
        $docParser->parse($this->object->getPayload());
111
112 2
        return $this->object;
113
    }
114
115
    /**
116
     * Reset the refers-to relations
117
     */
118 2
    protected function resetRefersToRelations()
119
    {
120 2
        $this->refersTo = [];
121
122
        // Run through all refers-to relations and delete them
123 2
        foreach ($this->object->findRelations([Relation::TYPE => Relation::REFERS_TO]) as $refersToRelation) {
124
            $this->object->deleteRelation($refersToRelation);
125 2
        }
126 2
    }
127
128
    /**
129
     * Reset the embeds relations
130
     */
131 2
    protected function resetEmbedsRelations()
132
    {
133 2
        $this->embeds = [];
134
135
        // Run through all refers-to relations and delete them
136 2
        foreach ($this->object->findRelations([Relation::TYPE => Relation::EMBEDS]) as $embedsRelation) {
137
            $this->object->deleteRelation($embedsRelation);
138 2
        }
139 2
    }
140
141
    /**
142
     * Process the CommonMark AST
143
     *
144
     * @param Document $document CommonMark AST
145
     * @return void
146
     */
147 2
    public function processDocument(Document $document)
148
    {
149
//        print_r($document);
150
151 2
        $walker = $document->walker();
152 2
        while ($event = $walker->next()) {
153 2
            $node = $event->getNode();
154
155
            // Process link starts as refers-to relations
156 2
            if (($node instanceof Link) && $event->isEntering()) {
157 1
                $this->addRefersToRelation(
158 1
                    $this->stripFragment($node->getUrl()),
159 1
                    empty($node->data['title']) ? null : $node->data['title']
160 1
                );
161 1
            }
162
163
            // Process image starts as embeds relations
164 2
            if (($node instanceof Image) && $event->isEntering()) {
165 1
                $this->addEmbedsRelation(
166 1
                    $this->stripFragment($node->getUrl()),
167 1
                    empty($node->data['title']) ? null : $node->data['title']
168 1
                );
169 1
            }
170 2
        }
171 2
    }
172
173
    /**
174
     * Add a refers-to relation
175
     *
176
     * @param string $url Referred URL
177
     * @param string $label Label
178
     */
179 1
    protected function addRefersToRelation($url, $label = null)
180
    {
181 1
        if (strlen($url) && !array_key_exists($url, $this->refersTo)) {
182 1
            $this->refersTo[$url] = true;
183 1
            $this->object->addRelation($this->getRelationString($url, $label), Relation::REFERS_TO);
184 1
        }
185 1
    }
186
187
    /**
188
     * Create a relation string
189
     *
190
     * @param string $url URL
191
     * @param string $label Label
192
     * @return string relation string
193
     */
194 1
    protected function getRelationString($url, $label)
195
    {
196 1
        $relationString = (strtolower(parse_url($url, PHP_URL_SCHEME)) == self::SCHEME_MAILTO)
197 1
            ? '<'.substr($url, strlen(self::SCHEME_MAILTO) + 1).'>'
198 1
            : $url;
199 1
        if (!empty($label)) {
200 1
            $relationString .= ' '.$label;
201 1
        }
202 1
        return $relationString;
203
    }
204
205
    /**
206
     * Strip off the fragment of an URL
207
     *
208
     * @param string $url URL
209
     * @return string URL with fragmet stripped
210
     */
211 1
    protected function stripFragment($url)
212
    {
213 1
        $fragment = parse_url($url, PHP_URL_FRAGMENT);
214 1
        if (!empty($fragment) && (substr($url, -strlen($fragment) - 1) == '#'.$fragment)) {
215 1
            $url = substr($url, 0, -strlen($fragment) - 1);
216 1
        }
217 1
        return $url;
218
    }
219
220
    /**
221
     * Add an embeds relation
222
     *
223
     * @param string $url Embedded URL
224
     * @param string $label Label
225
     */
226 1
    protected function addEmbedsRelation($url, $label = null)
227
    {
228 1
        if (strlen($url) && !array_key_exists($url, $this->embeds)) {
229 1
            $this->embeds[$url] = true;
230 1
            $this->object->addRelation($this->getRelationString($url, $label), Relation::EMBEDS);
231 1
        }
232 1
    }
233
}
234