Completed
Push — master ( 623a31...6b22a4 )
by Jose Luis
02:01
created

SetPrefixAndTableName::getTableName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
namespace Hechoenlaravel\JarvisFoundation\EntityGenerator\Middleware;
4
5
use League\Tactician\Middleware;
6
7
/**
8
 * Sets the prefix and table name for the table creation
9
 * Class SetPrefixAndTableName
10
 * @package Hechoenlaravel\JarvisFoundation\EntityGenerator\Middleware
11
 */
12
class SetPrefixAndTableName implements Middleware
13
{
14
    /**
15
     * @param object $command
16
     * @param callable $next
17
     *
18
     * @return mixed
19
     */
20
    public function execute($command, callable $next)
21
    {
22
        $command->prefix = $this->setPrefix($command);
23
        $command->table_name = $this->getTableName($command);
24
        return $next($command);
25
    }
26
27
28
    /**
29
     * Set the entity prefix
30
     * @param $command
31
     * @return mixed
32
     */
33
    protected function setPrefix($command)
34
    {
35
        if (empty($command->prefix) && empty($command->namespace)) {
36
            return null;
37
        }
38
        if (empty($command->prefix) && !empty($command->namespace)) {
39
            return $command->namespace;
40
        }
41
        return $command->prefix;
42
    }
43
44
45
    /**
46
     * set the entity table name
47
     * @param $command
48
     * @return string
49
     */
50
    protected function getTableName($command)
51
    {
52
        if(!empty($command->table_name)) {
53
            return $command->table_name;
54
        }
55
        if(empty($command->prefix)) {
56
            return $command->slug;
57
        }
58
        return $command->prefix.'_'.$command->slug;
59
    }
60
}
61