Passed
Push — main ( 99fa33...b48abb )
by Michael
01:09 queued 13s
created

ClassString   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 9
dl 0
loc 75
ccs 15
cts 15
cp 1
rs 10
c 2
b 0
f 0
wmc 8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A classExists() 0 3 1
A __construct() 0 3 1
A validate() 0 4 2
A instantiate() 0 3 1
A instantiateWith() 0 3 1
A interfaceExists() 0 3 1
A value() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of michael-rubel/laravel-value-objects. (https://github.com/michael-rubel/laravel-value-objects)
7
 *
8
 * @link https://github.com/michael-rubel/laravel-value-objects for the canonical source repository
9
 * @copyright Copyright (c) 2022 Michael Rubél. (https://github.com/michael-rubel/)
10
 * @license https://raw.githubusercontent.com/michael-rubel/laravel-value-objects/main/LICENSE.md MIT
11
 */
12
13
namespace MichaelRubel\ValueObjects\Collection\Complex;
14
15
use InvalidArgumentException;
16
use MichaelRubel\ValueObjects\ValueObject;
17
18
/**
19
 * "ClassString" object presenting a class string.
20
 *
21
 * @author Michael Rubél <[email protected]>
22
 *
23
 * @template TKey of array-key
24
 * @template TValue
25
 *
26
 * @method static static make(string $string)
27
 * @method static static from(string $string)
28
 *
29
 * @extends ValueObject<TKey, TValue>
30
 */
31
class ClassString extends ValueObject
32
{
33
    /**
34
     * Create a new instance of the value object.
35
     *
36
     * @param  string  $string
37
     */
38 16
    public function __construct(protected string $string)
39
    {
40 16
        $this->validate();
41
    }
42
43
    /**
44
     * Determine if the class exists for this class string.
45
     *
46
     * @return bool
47
     */
48 4
    public function classExists(): bool
49
    {
50 4
        return class_exists($this->value());
51
    }
52
53
    /**
54
     * Determine if the interface exists for this class string.
55
     *
56
     * @return bool
57
     */
58 4
    public function interfaceExists(): bool
59
    {
60 4
        return interface_exists($this->value());
61
    }
62
63
    /**
64
     * Instantiate the class string if possible.
65
     *
66
     * @param  array  $parameters
67
     *
68
     * @return object
69
     */
70 2
    public function instantiate(array $parameters = []): object
71
    {
72 2
        return app($this->value(), $parameters);
73
    }
74
75
    /**
76
     * Instantiate the class string if possible.
77
     *
78
     * @param  array  $parameters
79
     *
80
     * @return object
81
     */
82 1
    public function instantiateWith(array $parameters): object
83
    {
84 1
        return $this->instantiate($parameters);
85
    }
86
87
    /**
88
     * Get the object value.
89
     *
90
     * @return string
91
     */
92 16
    public function value(): string
93
    {
94 16
        return $this->string;
95
    }
96
97
    /**
98
     * Validate the value object data.
99
     *
100
     * @return void
101
     */
102 16
    protected function validate(): void
103
    {
104 16
        if (empty($this->value())) {
105 1
            throw new InvalidArgumentException('Class string cannot be empty.');
106
        }
107
    }
108
}
109