Test Setup Failed
Push — master ( d6fde7...1e848b )
by Php Easy Api
04:20
created

SchemaCapsule::checkErrors()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
namespace Migratio;
4
5
use Migratio\Contract\SchemaCapsuleContract;
6
7
class SchemaCapsule implements SchemaCapsuleContract
8
{
9
    /**
10
     * @var $config
0 ignored issues
show
Documentation Bug introduced by
The doc comment $config at position 0 could not be parsed: Unknown type name '$config' at position 0 in $config.
Loading history...
11
     */
12
    protected $config;
13
14
    /**
15
     * @var $file
0 ignored issues
show
Documentation Bug introduced by
The doc comment $file at position 0 could not be parsed: Unknown type name '$file' at position 0 in $file.
Loading history...
16
     */
17
    protected $file;
18
19
    /**
20
     * @var $table
0 ignored issues
show
Documentation Bug introduced by
The doc comment $table at position 0 could not be parsed: Unknown type name '$table' at position 0 in $table.
Loading history...
21
     */
22
    protected $table;
23
24
    /**
25
     * @var $wizardNamespace
0 ignored issues
show
Documentation Bug introduced by
The doc comment $wizardNamespace at position 0 could not be parsed: Unknown type name '$wizardNamespace' at position 0 in $wizardNamespace.
Loading history...
26
     */
27
    protected $wizardNamespace;
28
29
    /**
30
     * @var $wizardAlterNamespace
0 ignored issues
show
Documentation Bug introduced by
The doc comment $wizardAlterNamespace at position 0 could not be parsed: Unknown type name '$wizardAlterNamespace' at position 0 in $wizardAlterNamespace.
Loading history...
31
     */
32
    protected $wizardAlterNamespace;
33
34
    /**
35
     * SchemaCapsule constructor.
36
     * @param $config
37
     * @param $file
38
     * @param $table
39
     */
40
    public function __construct($config,$file,$table)
41
    {
42
        $this->config                       = $config;
43
        $this->file                         = $file;
44
        $this->table                        = $table;
45
        $this->wizardNamespace              = $this->getWizardNamespace().'\Wizard';
46
        $this->wizardAlterGroupNamespace    = $this->getWizardNamespace().'\WizardAlterGroup';
0 ignored issues
show
Bug Best Practice introduced by
The property wizardAlterGroupNamespace does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
    }
48
49
    /**
50
     * @param callable $callback
51
     * @return mixed|string
52
     */
53
    public function alter(callable $callback)
54
    {
55
        return $this->callbackWizardInstance(__FUNCTION__,$callback);
56
    }
57
58
    /**
59
     * @param callable $callback
60
     * @return mixed|string
61
     */
62
    public function create(callable $callback)
63
    {
64
        return $this->callbackWizardInstance(__FUNCTION__,$callback);
65
    }
66
67
    /**
68
     * @param $wizard
69
     */
70
    private function checkErrors($wizard)
71
    {
72
        if(count($wizard->getNames())!==count($wizard->getTypes())){
73
            $wizard->setError('name and types are not equal');
74
        }
75
    }
76
77
    /**
78
     * @return string
79
     */
80
    private function getWizardNamespace()
81
    {
82
        return 'Migratio\GrammarStructure\\'.ucfirst($this->config['database']['driver']).'\Wizard';
83
    }
84
85
    /**
86
     * @param $type
87
     * @return string
88
     */
89
    private function getWizardInstance($type)
90
    {
91
        $wizard = ($type=="create") ? $this->wizardNamespace : $this->wizardAlterGroupNamespace;
92
93
        $wizard = new $wizard;
94
95
        $wizard->schemaType($type);
96
97
        $wizard->setTable($this->table);
98
99
        $wizard->setFile($this->file);
100
101
        return $wizard;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $wizard returns the type object which is incompatible with the documented return type string.
Loading history...
102
    }
103
104
    /**
105
     * @param $method
106
     * @return string
107
     */
108
    private function callbackWizardInstance($method,callable $callback)
109
    {
110
        $wizardInstance = $this->getWizardInstance($method);
111
112
        $callbackReturn = call_user_func_array($callback,[$wizardInstance]);
0 ignored issues
show
Unused Code introduced by
The assignment to $callbackReturn is dead and can be removed.
Loading history...
113
114
        $this->checkErrors($wizardInstance);
115
116
        return $wizardInstance;
117
    }
118
}
119
120