Completed
Push — master ( 3bf76e...9f9c26 )
by Joschi
04:22
created

CommonMarkPayloadProcessor   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 176
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 97.01%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 176
ccs 65
cts 67
cp 0.9701
rs 10
wmc 24
lcom 1
cbo 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A setObject() 0 4 1
A processPayload() 0 16 1
A resetRefersToRelations() 0 9 2
A resetEmbedsRelations() 0 9 2
C processDocument() 0 23 8
A addRefersToRelation() 0 7 2
A getRelationString() 0 10 3
A stripFragment() 0 8 3
A addEmbedsRelation() 0 7 2
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 1
    public function setObject(AbstractCommonMarkObject $object)
89
    {
90 1
        $this->object = $object;
91 1
    }
92
93
    /**
94
     * Process the payload of a CommonMark object
95
     *
96
     * @return AbstractCommonMarkObject CommonMark object
97
     */
98 1
    public function processPayload()
99
    {
100
        // Reset all relevant relations
101 1
        $this->resetRefersToRelations();
102 1
        $this->resetEmbedsRelations();
103
104 1
        $env = Environment::createCommonMarkEnvironment();
105 1
        $env->addDocumentProcessor($this);
106
107
        // Parse and process the object payload
108
        /** @var DocParser $docParser */
109 1
        $docParser = Kernel::create(DocParser::class, [$env]);
110 1
        $docParser->parse($this->object->getPayload());
111
112 1
        return $this->object;
113
    }
114
115
    /**
116
     * Reset the refers-to relations
117
     */
118 1
    protected function resetRefersToRelations()
119
    {
120 1
        $this->refersTo = [];
121
122
        // Run through all refers-to relations and delete them
123 1
        foreach ($this->object->findRelations([Relation::TYPE => Relation::REFERS_TO]) as $refersToRelation) {
124
            $this->object->deleteRelation($refersToRelation);
125 1
        }
126 1
    }
127
128
    /**
129
     * Reset the embeds relations
130
     */
131 1
    protected function resetEmbedsRelations()
132
    {
133 1
        $this->embeds = [];
134
135
        // Run through all refers-to relations and delete them
136 1
        foreach ($this->object->findRelations([Relation::TYPE => Relation::EMBEDS]) as $embedsRelation) {
137
            $this->object->deleteRelation($embedsRelation);
138 1
        }
139 1
    }
140
141
    /**
142
     * Process the CommonMark AST
143
     *
144
     * @param Document $document CommonMark AST
145
     * @return void
146
     */
147 1
    public function processDocument(Document $document)
148
    {
149 1
        $walker = $document->walker();
150 1
        while ($event = $walker->next()) {
151 1
            $node = $event->getNode();
152
153
            // Process link starts as refers-to relations
154 1
            if (($node instanceof Link) && $event->isEntering()) {
155 1
                $this->addRefersToRelation(
156 1
                    $this->stripFragment($node->getUrl()),
157 1
                    empty($node->data['title']) ? null : $node->data['title']
158 1
                );
159 1
            }
160
161
            // Process image starts as embeds relations
162 1
            if (($node instanceof Image) && $event->isEntering()) {
163 1
                $this->addEmbedsRelation(
164 1
                    $this->stripFragment($node->getUrl()),
165 1
                    empty($node->data['title']) ? null : $node->data['title']
166 1
                );
167 1
            }
168 1
        }
169 1
    }
170
171
    /**
172
     * Add a refers-to relation
173
     *
174
     * @param string $url Referred URL
175
     * @param string $label Label
176
     */
177 1
    protected function addRefersToRelation($url, $label = null)
178
    {
179 1
        if (!array_key_exists($url, $this->refersTo)) {
180 1
            $this->refersTo[$url] = true;
181 1
            $this->object->addRelation($this->getRelationString($url, $label), Relation::REFERS_TO);
182 1
        }
183 1
    }
184
185
    /**
186
     * Create a relation string
187
     *
188
     * @param string $url URL
189
     * @param string $label Label
190
     * @return string relation string
191
     */
192 1
    protected function getRelationString($url, $label)
193
    {
194 1
        $relationString = (strtolower(parse_url($url, PHP_URL_SCHEME)) == self::SCHEME_MAILTO)
195 1
            ? '<'.substr($url, strlen(self::SCHEME_MAILTO) + 1).'>'
196 1
            : $url;
197 1
        if (!empty($label)) {
198 1
            $relationString .= ' '.$label;
199 1
        }
200 1
        return $relationString;
201
    }
202
203
    /**
204
     * Strip off the fragment of an URL
205
     *
206
     * @param string $url URL
207
     * @return string URL with fragmet stripped
208
     */
209 1
    protected function stripFragment($url)
210
    {
211 1
        $fragment = parse_url($url, PHP_URL_FRAGMENT);
212 1
        if (!empty($fragment) && (substr($url, -strlen($fragment) - 1) == '#'.$fragment)) {
213 1
            $url = substr($url, 0, -strlen($fragment) - 1);
214 1
        }
215 1
        return $url;
216
    }
217
218
    /**
219
     * Add an embeds relation
220
     *
221
     * @param string $url Embedded URL
222
     * @param string $label Label
223
     */
224 1
    protected function addEmbedsRelation($url, $label = null)
225
    {
226 1
        if (!array_key_exists($url, $this->embeds)) {
227 1
            $this->embeds[$url] = true;
228 1
            $this->object->addRelation($this->getRelationString($url, $label), Relation::EMBEDS);
229 1
        }
230 1
    }
231
}
232