Completed
Push — v2 ( 5e546d...a2e732 )
by Joschi
04:26
created

Parser::__invoke()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 7.0119

Importance

Changes 0
Metric Value
cc 7
eloc 17
nc 20
nop 3
dl 0
loc 26
ccs 15
cts 16
cp 0.9375
crap 7.0119
rs 6.7272
c 0
b 0
f 0
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\Micrometa\Application\Service\ExtractorService;
40
use Jkphl\Micrometa\Infrastructure\Factory\AlternateFactory;
41
use Jkphl\Micrometa\Infrastructure\Factory\DocumentFactory;
42
use Jkphl\Micrometa\Infrastructure\Factory\ParserFactory;
43
use Jkphl\Micrometa\Infrastructure\Factory\RelFactory;
44
use Jkphl\Micrometa\Ports\Item\ItemObjectModel;
45
use Jkphl\Micrometa\Ports\Item\ItemObjectModelInterface;
46
use League\Uri\Schemes\Http;
47
48
/**
49
 * Parser
50
 *
51
 * @package Jkphl\Micrometa
52
 * @subpackage Jkphl\Micrometa\Ports
53
 */
54
class Parser
55
{
56
    /**
57
     * Micro information formats
58
     *
59
     * @var int
60
     */
61
    protected $formats;
62
63
    /**
64
     * Parser constructor
65
     *
66
     * @param int $formats Micro information formats to extract
67
     * @api
68
     */
69 1
    public function __construct($formats = null)
70
    {
71 1
        $this->formats = $formats;
72 1
    }
73
74
    /**
75
     * Extract micro information items out of a URI or piece of source
76
     *
77
     * @param string $uri URI
78
     * @param string $source Source code
79
     * @param int $formats Micro information formats to extract
80
     * @return ItemObjectModelInterface Item object model
81
     */
82 1
    public function __invoke($uri, $source = null, $formats = null)
83
    {
84
        // If source code has been passed in
85 1
        $dom = (($source !== null) && strlen(trim($source))) ?
86 1
            DocumentFactory::createFromString($source) : DocumentFactory::createFromUri($uri);
87
88
        // Run through all format parsers
89 1
        $items = $rels = $alternates = [];
90 1
        $extractor = new ExtractorService();
91 1
        foreach (ParserFactory::createParsersFromFormats(
92 1
            intval($formats ?: $this->formats),
93 1
            Http::createFromString($uri)
94
        ) as $parser) {
95 1
            $results = $extractor->extract($dom, $parser);
96 1
            $items += $results->getItems();
97 1
            $extra = $results->getExtra();
98 1
            if (!empty($extra['rels'])) {
99
                $rels += RelFactory::createFromParserResult($extra['rels']);
100
            }
101 1
            if (!empty($extra['alternates'])) {
102 1
                $alternates += AlternateFactory::createFromParserResult($extra['alternates']);
103
            }
104
        }
105
106 1
        return new ItemObjectModel();
107
    }
108
}
109