ResolveModelName   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 16
dl 0
loc 63
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 4 1
A __construct() 0 4 1
A getTableCamelCase() 0 9 1
A getClassName() 0 6 2
1
<?php
2
3
namespace Sfneal\Models\Actions;
4
5
use Sfneal\Actions\Action;
6
use Sfneal\Helpers\Laravel\LaravelHelpers;
7
use Sfneal\Helpers\Strings\StringHelpers;
8
use Sfneal\Models\Model;
9
10
class ResolveModelName extends Action
11
{
12
    /**
13
     * @var Model|string
14
     */
15
    private $model;
16
17
    /**
18
     * @var bool
19
     */
20
    private $short;
21
22
    /**
23
     * ResolveModelName constructor.
24
     *
25
     * @param  Model|string  $model
26
     * @param  bool  $short
27
     */
28
    public function __construct($model, bool $short = true)
29
    {
30
        $this->model = $model;
31
        $this->short = $short;
32
    }
33
34
    /**
35
     * Retrieve the Model class's short name (without namespace).
36
     *
37
     * @return string
38
     */
39
    public function execute(): string
40
    {
41
        // Split string on upper case characters
42
        return implode(' ', (new StringHelpers($this->getClassName()))->camelCaseSplit());
43
    }
44
45
    /**
46
     * Retrieve the name from the Model's class name or the table name.
47
     *
48
     * @return string
49
     */
50
    private function getClassName(): string
51
    {
52
        return LaravelHelpers::getClassName(
53
            $this->model,
54
            $this->short,
55
            ! is_string($this->model) ? $this->getTableCamelCase() : null
56
        );
57
    }
58
59
    /**
60
     * Convert table name to a CamelCase string for consistency with Model names.
61
     *
62
     * @return string
63
     */
64
    private function getTableCamelCase(): string
65
    {
66
        return implode(
67
            '',
68
            array_map(
69
                function (string $piece) {
70
                    return ucfirst($piece);
71
                },
72
                explode('_', $this->model->getTable())
73
            )
74
        );
75
    }
76
}
77