SsrRender::filter()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 7
cts 9
cp 0.7778
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 9
nc 3
nop 2
crap 3.0987
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: polidog
5
 * Date: 2017/02/22.
6
 */
7
8
namespace Polidog\SsrBundle\Render;
9
10
use Koriym\Baracoa\BaracoaInterface;
11
use Polidog\SsrBundle\Annotations\Ssr;
12
13
class SsrRender implements SsrRenderInterface
14
{
15
    /**
16
     * @var BaracoaInterface
17
     */
18
    private $baracoa;
19
20
    /**
21
     * SsrRender constructor.
22
     *
23
     * @param BaracoaInterface $baracoa
24
     */
25 1
    public function __construct(BaracoaInterface $baracoa)
26
    {
27 1
        $this->baracoa = $baracoa;
28 1
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 1
    public function render(Ssr $ssr, array $parameters)
34
    {
35 1
        return $this->baracoa->render(
36 1
            $ssr->getApp(),
37 1
            $this->filter($ssr->getState(), $parameters),
38 1
            $this->filter($ssr->getMetas(), $parameters)
39
        );
40
    }
41
42 1
    private function filter(array $keys, array $parameters)
43
    {
44 1
        if ($keys === ['*']) {
45
            return $parameters;
46
        }
47
48 1
        $errorKeys = array_keys(array_diff(array_values($keys), array_keys($parameters)));
49 1
        if ($errorKeys) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errorKeys of type integer[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
50
            throw new \LogicException(implode(',', $errorKeys));
51
        }
52
53 1
        return array_filter((array) $parameters, function($key) use ($keys) {
54 1
            return in_array($key, $keys);
55 1
        }, ARRAY_FILTER_USE_KEY);
56
    }
57
}
0 ignored issues
show
Coding Style introduced by
As per coding style, files should not end with a newline character.

This check marks files that end in a newline character, i.e. an empy line.

Loading history...
58