Completed
Push — master ( 328857...405b9d )
by Basenko
03:28
created

Protocol   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 58.06%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 91
ccs 18
cts 31
cp 0.5806
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
getRawFields() 0 1 ?
A __construct() 0 10 1
B parseValue() 0 12 6
A __call() 0 12 2
A twitter() 0 4 1
A opengraph() 0 4 1
A meta() 0 4 1
A ignoreStored() 0 6 1
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 9
    public function __construct(Seoable $model)
36
    {
37 9
        $this->model = $model;
38 9
        $this->modelSeoData = (array) $this->model->getSeoData();
39
40 9
        $this->seoTools = resolve('seotools');
41 9
        $this->metaService = resolve('seotools.metatags');
42 9
        $this->openGraphService = resolve('seotools.opengraph');
43 9
        $this->twitterCardService = resolve('seotools.twitter'); //TODO: resolve method ability
44 9
    }
45
46
    /**
47
     * @param array|string $value
48
     * @param \MadWeb\Seoable\Fields\Field|string $type
49
     * @return mixed
50
     */
51 9
    protected function parseValue($value, $type)
52
    {
53 9
        $raw_field = $this->isStoredFieldsIgnores ?
54
            null :
55 9
            $this->getRawFields()[snake_case(class_basename($type))] ?? null;
56
57 9
        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 9
            $type = $type instanceof Field ? $type : new $type($value, $this->model);
59
        }
60
61 9
        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 3
    public function twitter(): TwitterCard
78
    {
79 3
        return new TwitterCard($this->model);
80
    }
81
82 3
    public function opengraph(): OpenGraph
83
    {
84 3
        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