Protocol   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 93.55%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 4
dl 0
loc 91
ccs 29
cts 31
cp 0.9355
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
getRawFields() 0 1 ?
A __construct() 0 10 1
A 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 Illuminate\Database\Eloquent\Model;
7
use Illuminate\Support\Str;
8
use MadWeb\Seoable\Contracts\Seoable;
9
use MadWeb\Seoable\Fields\Field;
10
11
abstract class Protocol
12
{
13
    /** @var Model|\MadWeb\Seoable\Contracts\Seoable */
14
    protected $model;
15
16
    /** @var array */
17
    protected $modelSeoData;
18
19
    /** @var \Artesaos\SEOTools\SEOMeta */
20
    protected $metaService = null;
21
22
    /** @var \Artesaos\SEOTools\OpenGraph */
23
    protected $openGraphService = null;
24
25
    /** @var \Artesaos\SEOTools\TwitterCards */
26
    protected $twitterCardService = null;
27
28
    /** @var \Artesaos\SEOTools\SEOTools */
29
    protected $seoTools = null;
30
31
    protected $isRaw = false;
32
33
    protected $isStoredFieldsIgnores = false;
34
35
    /** @param Model|\MadWeb\Seoable\Contracts\Seoable $model */
36 21
    public function __construct(Seoable $model)
37
    {
38 21
        $this->model = $model;
39 21
        $this->modelSeoData = (array) $this->model->getSeoData();
40
41 21
        $this->seoTools = resolve('seotools');
42 21
        $this->metaService = resolve('seotools.metatags');
43 21
        $this->openGraphService = resolve('seotools.opengraph');
44 21
        $this->twitterCardService = resolve('seotools.twitter'); //TODO: resolve method ability
45 21
    }
46
47
    /**
48
     * @param array|string $value
49
     * @param \MadWeb\Seoable\Fields\Field|string $type
50
     * @return mixed
51
     */
52 18
    protected function parseValue($value, $type)
53
    {
54 18
        $raw_field = $this->isStoredFieldsIgnores ?
55 3
            null :
56 18
            $this->getRawFields()[Str::snake(class_basename($type))] ?? null;
57
58 18
        if (! $raw_field and ! $this->isRaw) {
59 12
            $type = $type instanceof Field ? $type : new $type($value, $this->model);
60
        }
61
62 18
        return $raw_field ?? ($this->isRaw ? $value : $type->getValue());
63
    }
64
65 6
    public function __call($name, $arguments)
66
    {
67 6
        if (Str::endsWith($name, 'Raw')) {
68 3
            $this->isRaw = true;
69 3
            $this->{mb_strstr($name, 'Raw', true)}(...$arguments);
70 3
            $this->isRaw = false;
71
72 3
            return $this;
73
        }
74
75 3
        throw new BadMethodCallException;
76
    }
77
78 3
    public function twitter(): TwitterCard
79
    {
80 3
        return new TwitterCard($this->model);
81
    }
82
83 3
    public function opengraph(): OpenGraph
84
    {
85 3
        return new OpenGraph($this->model);
86
    }
87
88
    public function meta(): Meta
89
    {
90
        return new Meta($this->model);
91
    }
92
93 3
    public function ignoreStored()
94
    {
95 3
        $this->isStoredFieldsIgnores = true;
96
97 3
        return $this;
98
    }
99
100
    abstract protected function getRawFields(): array;
101
}
102