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

SetPrefixAndTableName   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 0
dl 0
loc 49
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 6 1
B setPrefix() 0 10 5
A getTableName() 0 10 3
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