Completed
Push — v2 ( 9de304...bf6a94 )
by Joschi
05:08
created

Parser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 8

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 8
dl 0
loc 64
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 2
B __invoke() 0 27 6
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\ItemObjectModel;
45
use Jkphl\Micrometa\Ports\Item\ItemObjectModelInterface;
46
use League\Uri\Schemes\Http;
47
use Psr\Log\LoggerInterface;
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
     * Logger
65
     *
66
     * @var LoggerInterface
67
     */
68
    protected $logger;
69
70
    /**
71
     * Parser constructor
72
     *
73
     * @param int $formats Micro information formats to extract
74
     * @param LoggerInterface|null $logger PSR-3 compatible logger
75
     * @api
76
     */
77 2
    public function __construct($formats = null, LoggerInterface $logger = null)
78
    {
79 2
        $this->formats = $formats;
80 2
        $this->logger = $logger ?: new ExceptionLogger();
81 2
    }
82
83
    /**
84
     * Extract micro information items out of a URI or piece of source
85
     *
86
     * @param string $uri URI
87
     * @param string $source Source code
88
     * @param int $formats Micro information formats to extract
89
     * @return ItemObjectModelInterface Item object model
90
     */
91 2
    public function __invoke($uri, $source = null, $formats = null)
92
    {
93 2
        $items = [];
94
95
        try {
96
            // If source code has been passed in
97 2
            $dom = (($source !== null) && strlen(trim($source))) ?
98 2
                Dom::createFromString($source) : Dom::createFromUri($uri);
99
100
            // Run through all format parsers
101 2
            $extractor = new ExtractorService();
102 2
            foreach (ParserFactory::createParsersFromFormats(
103 2
                intval($formats ?: $this->formats),
104 2
                Http::createFromString($uri),
105 2
                $this->logger
106 2
            ) as $parser) {
107 2
                $results = $extractor->extract($dom, $parser);
108 1
                $items = array_merge($items, ItemFactory::createFromApplicationItems($results->getItems()));
109 1
            }
110
111
            // In case of exceptions: Log if possible
112 2
        } catch (\Exception $e) {
113 1
            $this->logger->critical($e->getMessage(), ['exception' => $e]);
114
        }
115
116 1
        return new ItemObjectModel($items);
117
    }
118
}
119