Completed
Push — v2 ( e81eda...b3c0e1 )
by Joschi
04:43
created

Parser::__invoke()   C

Complexity

Conditions 7
Paths 20

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 7.392

Importance

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