Completed
Push — master ( 8f5f7c...7faed5 )
by Basenko
04:23
created

Protocol::parseValue()   B

Complexity

Conditions 6
Paths 12

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
c 0
b 0
f 0
rs 8.8571
cc 6
eloc 7
nc 12
nop 2
1
<?php
2
3
namespace MadWeb\Seoable\Protocols;
4
5
use BadMethodCallException;
6
use MadWeb\Seoable\Fields\Field;
7
use MadWeb\Seoable\Contracts\Seoable;
8
use Illuminate\Database\Eloquent\Model;
9
10
abstract class Protocol
11
{
12
    /** @var Model|\MadWeb\Seoable\Contracts\Seoable */
13
    protected $model;
14
15
    /** @var array */
16
    protected $modelSeoData;
17
18
    /** @var \Artesaos\SEOTools\SEOMeta */
19
    protected $metaService = null;
20
21
    /** @var \Artesaos\SEOTools\OpenGraph */
22
    protected $openGraphService = null;
23
24
    /** @var \Artesaos\SEOTools\TwitterCards */
25
    protected $twitterCardService = null;
26
27
    /** @var \Artesaos\SEOTools\SEOTools */
28
    protected $seoTools = null;
29
30
    protected $isRaw = false;
31
32
    protected $isStoredFieldsIgnores = false;
33
34
    /** @param Model|\MadWeb\Seoable\Contracts\Seoable $model */
35
    public function __construct(Seoable $model)
36
    {
37
        $this->model = $model;
38
        $this->modelSeoData = (array) $this->model->getSeoData();
39
40
        $this->seoTools = resolve('seotools');
41
        $this->metaService = resolve('seotools.metatags');
42
        $this->openGraphService = resolve('seotools.opengraph');
43
        $this->twitterCardService = resolve('seotools.twitter'); //TODO: resolve method ability
44
    }
45
46
    /**
47
     * @param array|string $value
48
     * @param \MadWeb\Seoable\Fields\Field|string $type
49
     * @return mixed
50
     */
51
    protected function parseValue($value, $type)
52
    {
53
        $raw_field = $this->isStoredFieldsIgnores ?
54
            null :
55
            $this->getRawFields()[snake_case(class_basename($type))] ?? null;
56
57
        if (! $raw_field and ! $this->isRaw) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
58
            $type = $type instanceof Field ? $type : new $type($value, $this->model);
59
        }
60
61
        return $raw_field ?? ($this->isRaw ? $value : $type->getValue());
62
    }
63
64
    public function __call($name, $arguments)
65
    {
66
        if (ends_with($name, 'Raw')) {
67
            $this->isRaw = true;
68
            $this->{mb_strstr($name, 'Raw', true)}(...$arguments);
69
            $this->isRaw = false;
70
71
            return $this;
72
        }
73
74
        throw new BadMethodCallException;
75
    }
76
77
    public function twitter(): TwitterCard
78
    {
79
        return new TwitterCard($this->model);
80
    }
81
82
    public function opengraph(): OpenGraph
83
    {
84
        return new OpenGraph($this->model);
85
    }
86
87
    public function meta(): Meta
88
    {
89
        return new Meta($this->model);
90
    }
91
92
    public function ignoreStored()
93
    {
94
        $this->isStoredFieldsIgnores = true;
95
96
        return $this;
97
    }
98
99
    abstract protected function getRawFields(): array;
100
}
101