MakeCriteriaCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 104
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 37
dl 0
loc 104
rs 10
c 0
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A replaceCriteriaNamespace() 0 3 1
A replaceCriteriaName() 0 3 1
A compileCriteriaStub() 0 8 1
A populateValuesForProperties() 0 12 3
A handle() 0 11 1
A createCriteria() 0 19 3
1
<?php
2
3
namespace OkayBueno\Repositories\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Filesystem\Filesystem;
7
8
/**
9
 * Class MakeCriteriaCommand
10
 * @package OkayBueno\Repositories\Commands
11
 */
12
class MakeCriteriaCommand extends MakeBaseCommand
13
{
14
15
    protected $signature = 'make:criteria {criteria} {--implementation}';
16
    protected $description = 'Create a new criteria.';
17
18
    private $implementation;
19
    private $criteriaClassName;
20
    private $criteriaClassNamespace;
21
    private $criteriaFolder;
22
23
    /**
24
     * Execute the console command.
25
     *
26
     * @return mixed
27
     */
28
    public function handle()
29
    {
30
        $criteria = $this->argument('criteria');
31
        $implementation = strtolower( $this->option('implementation') );
32
33
        $this->populateValuesForProperties( $criteria, $implementation );
34
        $this->createCriteria();
35
36
        $this->info('Generating autoload...');
37
        $this->composer->dumpAutoloads();
38
        $this->info('Done!');
39
    }
40
41
42
    /**
43
     * @param $criteria
44
     * @param $implementation
45
     */
46
    protected function populateValuesForProperties( $criteria, $implementation )
47
    {
48
        $this->implementation = $implementation ? $implementation : $this->findDefaultImplementation();
49
50
        $criteriaNameForFolder = str_replace('\\', '/', $criteria );
51
        $this->criteriaFolder = ucfirst( $this->implementation );
0 ignored issues
show
Bug introduced by
It seems like $this->implementation can also be of type array; however, parameter $str of ucfirst() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

51
        $this->criteriaFolder = ucfirst( /** @scrutinizer ignore-type */ $this->implementation );
Loading history...
52
53
        $folder = pathinfo( $criteriaNameForFolder , PATHINFO_DIRNAME );;
54
        if ( $folder ) $this->criteriaFolder .= '/'.$folder;
55
56
        $this->criteriaClassName = pathinfo( $criteriaNameForFolder , PATHINFO_FILENAME );
57
        $this->criteriaClassNamespace = rtrim( config( 'repositories.criterias_namespace' ), '\\' ) . '\\' . str_replace( '/', '\\', $this->criteriaFolder );
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

57
        $this->criteriaClassNamespace = rtrim( /** @scrutinizer ignore-call */ config( 'repositories.criterias_namespace' ), '\\' ) . '\\' . str_replace( '/', '\\', $this->criteriaFolder );
Loading history...
58
    }
59
60
    /**
61
     *
62
     */
63
    protected function createCriteria()
64
    {
65
        $basePath = config( 'repositories.criterias_path' );
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

65
        $basePath = /** @scrutinizer ignore-call */ config( 'repositories.criterias_path' );
Loading history...
66
67
        if ( $this->criteriaFolder ) $basePath .= '/'.$this->criteriaFolder;
68
69
        $this->makeDirectory( $basePath );
70
71
        $criteriaFilePath = $basePath .'/'.$this->criteriaClassName.'.php';
72
73
        if ( !$this->filesystem->exists( $criteriaFilePath ) )
74
        {
75
            // Read the stub and replace
76
            $this->filesystem->put( $criteriaFilePath, $this->compileCriteriaStub() );
77
            $this->info("Criteria '$this->criteriaClassName' created successfully in '$criteriaFilePath'.");
78
            $this->composer->dumpAutoloads();
79
        } else
80
        {
81
            $this->error("The criteria '$this->criteriaClassName' already exists in '$criteriaFilePath.");
82
        }
83
    }
84
85
86
    /**
87
     * @return mixed|string
88
     */
89
    protected function compileCriteriaStub( )
90
    {
91
        $stub = $this->filesystem->get(__DIR__ . '/../stubs/eloquent-criteria.stub');
92
93
        $stub = $this->replaceCriteriaNamespace( $stub );
94
        $stub = $this->replaceCriteriaName( $stub );
95
96
        return $stub;
97
    }
98
99
100
    /**
101
     * @param $stub
102
     * @return mixed
103
     */
104
    private function replaceCriteriaNamespace( $stub )
105
    {
106
        return str_replace('{{criteriaNamespace}}', $this->criteriaClassNamespace, $stub);
107
    }
108
109
    /**
110
     * @param $stub
111
     * @return mixed
112
     */
113
    private function replaceCriteriaName( $stub )
114
    {
115
        return str_replace('{{criteriaClassName}}', $this->criteriaClassName, $stub);
116
    }
117
118
}