Completed
Push — v2 ( 3135e6...5d3e3a )
by Joschi
04:42 queued 10s
created

Parser::__invoke()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

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