Passed
Push — master ( 29bd61...08926b )
by Brent
06:03
created

AbstractAdapter   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 59
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A transform() 0 14 3
A getData() 0 9 2
1
<?php
2
3
namespace Brendt\Stitcher\Adapter;
4
5
use Brendt\Stitcher\Config;
6
use Brendt\Stitcher\factory\ParserFactory;
7
use Brendt\Stitcher\Site\Page;
8
9
/**
10
 * The AbstractAdapter class provides a base for adapters who need to parse template variables.
11
 */
12
abstract class AbstractAdapter implements Adapter
13
{
14
15
    /**
16
     * @var ParserFactory
17
     */
18
    protected $parserFactory;
19
20
    /**
21
     * Construct the adapter and set the parser factory variable.
22
     *
23
     * @see \Brendt\Stitcher\Factory\ParserFactory
24
     */
25
    public function __construct() {
26
        $this->parserFactory = Config::getDependency('factory.parser');
27
    }
28
29
    /**
30
     * @param Page|Page[] $pages
31
     * @param mixed  $filter
32
     *
33
     * @return Page[]
34
     */
35
    public function transform($pages, $filter = null) : array {
36
        if (!is_array($pages)) {
37
            $pages = [$pages];
38
        }
39
40
        /** @var Page[] $result */
41
        $result = [];
42
43
        foreach ($pages as $page) {
44
            $result += $this->transformPage($page, $filter);
45
        }
46
47
        return $result;
48
    }
49
50
    /**
51
     * This function will get the parser based on the value provided.
52
     * This value is parsed by the parser, or returned if no suitable parser was found.
53
     *
54
     * @param $value
55
     *
56
     * @return mixed
57
     *
58
     * @see \Brendt\Stitcher\Factory\ParserFactory
59
     */
60
    protected function getData($value) {
61
        $parser = $this->parserFactory->getParser($value);
62
63
        if (!$parser) {
64
            return $value;
65
        }
66
67
        return $parser->parse($value);
68
    }
69
70
}
71