FieldDoesNotExtendState::make()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 6
rs 10
cc 1
nc 1
nop 3
1
<?php
2
3
namespace Spatie\ModelStates\Exceptions;
4
5
use Facade\IgnitionContracts\BaseSolution;
6
use Facade\IgnitionContracts\ProvidesSolution;
7
use Facade\IgnitionContracts\Solution;
8
9
class FieldDoesNotExtendState extends InvalidConfig implements ProvidesSolution
10
{
11
    /** @var string */
12
    protected $field;
13
14
    /** @var string */
15
    protected $expectedStateClass;
16
17
    /** @var string */
18
    protected $actualClass;
19
20
    public static function make(string $field, string $expectedStateClass, string $actualClass): self
21
    {
22
        return (new static("State field `{$field}` expects state to be of type `{$expectedStateClass}`, instead got `{$actualClass}`"))
23
            ->setField($field)
24
            ->setExpectedStateClass($expectedStateClass)
25
            ->setActualClass($actualClass);
26
    }
27
28
    public function setField(string $field): self
29
    {
30
        $this->field = $field;
31
32
        return $this;
33
    }
34
35
    public function setExpectedStateClass(string $expectedStateClass): self
36
    {
37
        $this->expectedStateClass = $expectedStateClass;
38
39
        return $this;
40
    }
41
42
    public function setActualClass(string $actualClass): self
43
    {
44
        $this->actualClass = $actualClass;
45
46
        return $this;
47
    }
48
49
    public function getSolution(): Solution
50
    {
51
        return BaseSolution::create("State field `{$this->field}` is the wrong type")
52
            ->setSolutionDescription("Make sure that states for state field `{$this->field}` extend `{$this->expectedStateClass}`, not `{$this->actualClass}`")
53
            ->setDocumentationLinks([
54
                'Configuring states' => 'https://docs.spatie.be/laravel-model-states/v1/working-with-states/01-configuring-states/',
55
            ]);
56
    }
57
}
58