Passed
Pull Request — master (#407)
by Kirill
11:08 queued 03:58
created

ClassBasedTrait   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 44
rs 10
wmc 5

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getClass() 0 3 1
A checkClass() 0 5 2
A setClass() 0 7 2
1
<?php
2
3
/**
4
 * This file is part of Spiral Framework package.
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Storage\Config\DTO\Traits;
13
14
use Spiral\Storage\Exception\StorageException;
15
16
/**
17
 * Trait for dto based on class usage
18
 */
19
trait ClassBasedTrait
20
{
21
    /**
22
     * @var string|null
23
     */
24
    protected $class = null;
25
26
    public function getClass(): string
27
    {
28
        return $this->class;
29
    }
30
31
    /**
32
     * Set class for DTO and check if class exists
33
     *
34
     * @param string $class
35
     * @param string|null $exceptionMsg
36
     *
37
     * @return static
38
     *
39
     * @throws StorageException
40
     */
41
    public function setClass(string $class, ?string $exceptionMsg = null): self
42
    {
43
        $this->checkClass($class, $exceptionMsg ?: '');
44
45
        $this->class = $class;
46
47
        return $this;
48
    }
49
50
    /**
51
     * Check if class exists
52
     *
53
     * @param string $class
54
     * @param string $errorPostfix
55
     *
56
     * @throws StorageException
57
     */
58
    protected function checkClass(string $class, string $errorPostfix): void
59
    {
60
        if (!class_exists($class)) {
61
            throw new StorageException(
62
                \sprintf('Class `%s` not exists. %s', $class, $errorPostfix)
63
            );
64
        }
65
    }
66
}
67