Completed
Push — v2 ( b41681...2062ad )
by Joschi
04:41
created

Parser::createDom()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 3
nc 4
nop 2
crap 3
1
<?php
2
3
/**
4
 * micrometa
5
 *
6
 * @category Jkphl
7
 * @package Jkphl\Micrometa
8
 * @subpackage Jkphl\Micrometa\Ports
9
 * @author Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright Copyright © 2017 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 © 2017 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 Jkphl\Micrometa\Ports;
38
39
use Jkphl\Domfactory\Ports\Dom;
40
use Jkphl\Micrometa\Application\Service\ExtractorService;
41
use Jkphl\Micrometa\Infrastructure\Factory\ItemFactory;
42
use Jkphl\Micrometa\Infrastructure\Factory\ParserFactory;
43
use Jkphl\Micrometa\Infrastructure\Logger\ExceptionLogger;
44
use Jkphl\Micrometa\Ports\Item\ItemInterface;
45
use Jkphl\Micrometa\Ports\Item\ItemObjectModel;
46
use Jkphl\Micrometa\Ports\Item\ItemObjectModelInterface;
47
use League\Uri\Schemes\Http;
48
use Psr\Log\LoggerInterface;
49
50
/**
51
 * Parser
52
 *
53
 * @package Jkphl\Micrometa
54
 * @subpackage Jkphl\Micrometa\Ports
55
 */
56
class Parser
57
{
58
    /**
59
     * Micro information formats
60
     *
61
     * @var int
62
     */
63
    protected $formats;
64
    /**
65
     * Logger
66
     *
67
     * @var LoggerInterface
68
     */
69
    protected $logger;
70
71
    /**
72
     * Parser constructor
73
     *
74
     * @param int $formats Micro information formats to extract
75
     * @param LoggerInterface|null $logger PSR-3 compatible logger
76
     * @api
77
     */
78 2
    public function __construct($formats = null, LoggerInterface $logger = null)
79
    {
80 2
        $this->formats = $formats;
81 2
        $this->logger = $logger ?: new ExceptionLogger();
82 2
    }
83
84
    /**
85
     * Extract micro information items out of a URI or piece of source
86
     *
87
     * @param string $uri URI
88
     * @param string|null $source Source code
89
     * @param int $formats Micro information formats to extract
90
     * @return ItemObjectModelInterface Item object model
91
     */
92 2
    public function __invoke($uri, $source = null, $formats = null)
93
    {
94 2
        $items = [];
95
96
        try {
97 2
            $parsers = ParserFactory::createParsersFromFormats(
98 2
                intval($formats ?: $this->formats),
99 2
                Http::createFromString($uri),
100 2
                $this->logger
101
            );
102 2
            $items = $this->extractItems($this->createDom($uri, $source), $parsers);
103
104
            // In case of exceptions: Log if possible
105 1
        } catch (\Exception $e) {
106 1
            $this->logger->critical($e->getMessage(), ['exception' => $e]);
107
        }
108
109 1
        return new ItemObjectModel($items);
110
    }
111
112
    /**
113
     * Create the DOM document to parse
114
     *
115
     * @param string $uri URI
116
     * @param string|null $source Source code
117
     * @return \DOMDocument DOM document
118
     */
119 2
    protected function createDom($uri, $source = null)
120
    {
121 2
        return (($source !== null) && strlen(trim($source))) ?
122 2
            Dom::createFromString($source) : Dom::createFromUri($uri);
123
    }
124
125
    /**
126
     * Extract all items from a DOM using particular parsers
127
     *
128
     * @param \DOMDocument $dom DOM document
129
     * @param \Generator $parsers Parsers
130
     * @return ItemInterface[] Items
131
     */
132 2
    protected function extractItems(\DOMDocument $dom, \Generator $parsers)
133
    {
134 2
        $items = [];
135 2
        $extractor = new ExtractorService();
136 2
        foreach ($parsers as $parser) {
137 2
            $results = $extractor->extract($dom, $parser);
138 1
            $items = array_merge($items, ItemFactory::createFromApplicationItems($results->getItems()));
139
        }
140 1
        return $items;
141
    }
142
}
143