Test Failed
Branch 2.0.0 (9237fa)
by Chubarov
06:45
created

KernelBpm::getCurl()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace agoalofalife\bpm;
3
4
use agoalofalife\bpm\Actions\Create;
5
use agoalofalife\bpm\Actions\Delete;
6
use agoalofalife\bpm\Actions\Read;
7
use agoalofalife\bpm\Actions\Update;
8
use agoalofalife\bpm\Contracts\Action;
9
use agoalofalife\bpm\Contracts\Authentication;
10
use agoalofalife\bpm\Contracts\Handler;
11
use agoalofalife\bpm\Contracts\SourceConfiguration;
12
use agoalofalife\bpm\Handlers\JsonHandler;
13
use agoalofalife\bpm\Handlers\XmlHandler;
14
use agoalofalife\bpm\ServiceProviders\ActionsServiceProviders;
15
use agoalofalife\bpm\ServiceProviders\AuthenticationServiceProvider;
16
use agoalofalife\bpm\ServiceProviders\ClientServiceProvider;
17
use agoalofalife\bpm\ServiceProviders\ConfigurationServiceProvider;
18
use Assert\Assertion;
19
use Assert\AssertionFailedException;
20
use GuzzleHttp\ClientInterface;
21
22
/**
23
 * Class KernelBpm
24
 * @property array    $action list action
25
 * @property array    $handlers list handler xml | json
26
 * @property string   $collection Name collection in Bpm
27
 * @property Action   $currentAction current action which use in client code
28
 * @property Handler  $currentHandler current handler which use in client code
29
 * @property string   $url
30
 * @property string   $prefixConfiguration prefix for set and search in Repository class
31
 * @property array    $serviceProviders list ServiceProvider for bootstrap
32
 *
33
 * @package agoalofalife\bpm
34
 */
35
class KernelBpm
36
{
37
    protected $action = [
38
        'create' =>  Create::class,
39
        'read'   =>  Read::class,
40
        'update' =>  Update::class,
41
        'delete' =>  Delete::class,
42
    ];
43
44
    protected $handlers = [
45
      'xml'  => XmlHandler::class,
46
      'json' => JsonHandler::class,
47
    ];
48
49
    protected $collection;
50
    protected $currentAction;
51
    protected $currentHandler;
52
    protected $url;
53
54
    /**
55
     * prefix name configuration
56
     */
57
    protected $prefixConfiguration;
58
59
    /**
60
     * list providers for pre bootstrapping packages
61
     */
62
    protected $serviceProviders = [
63
        ConfigurationServiceProvider::class,
64
        ActionsServiceProviders::class,
65
        AuthenticationServiceProvider::class,
66
        ClientServiceProvider::class,
67
    ];
68
69
    public function __construct()
70
    {
71
        $this->bootstrapping();
72
    }
73
74
    /**
75
     * Auth in Bpm online
76
     * @return  void
77
     */
78
    public function authentication()
79
    {
80
        $auth = app()->make(Authentication::class);
81
        $auth->setConfig(config($this->prefixConfiguration));
82
        $auth->auth();
83
    }
84
85
    /**
86
     * Get list actions
87
     * @return array
88
     */
89
    public function getListActions()
90
    {
91
        return $this->action;
92
    }
93
94
    /**
95
     * @return string
96
     */
97
    public function getPrefixConfig()
98
    {
99
        return $this->prefixConfiguration;
100
    }
101
102
    /**
103
     * @param SourceConfiguration $configuration
104
     */
105
    public function loadConfiguration(SourceConfiguration $configuration)
106
    {
107
        config()->set(  $this->prefixConfiguration = $configuration->getName(), $configuration->get());
108
    }
109
110
    /**
111
     * @param       $key
112
     * @param array $array
113
     * @return void
114
     */
115
    public function setConfigManually($key, array $array)
116
    {
117
        $this->prefixConfiguration = $key;
118
        config()->set($key, $array);
119
    }
120
121
    /**
122
     * @return Handler
123
     */
124
    public function getHandler()
125
    {
126
        return $this->currentHandler;
127
    }
128
129
    /**
130
     * Set the response handler
131
     * @param string $typeHandler default xml
132
     * @return Action
133
     */
134
    public function setHandler($typeHandler = 'xml')
135
    {
136
        Assertion::keyIsset($this->handlers, $typeHandler);
137
        $this->currentHandler = app()->make( $this->handlers[$typeHandler] );
138
        return  $this->currentHandler;
139
    }
140
141
    /**
142
     * @return Action
143
     */
144
    public function getAction()
145
    {
146
        return $this->currentAction;
147
    }
148
149
    /**
150
     * @param $typeAction string
151
     * @return Action
152
     */
153
    public function setAction($typeAction)
154
    {
155
        Assertion::keyIsset($this->action, $typeAction);
156
157
        $this->currentAction  =  app()->make( $this->action[$typeAction] );
158
159
        return $this->currentAction;
160
    }
161
162
    /**
163
     * Example action parameter 'read:json'
164
     * @param string $action
165
     * @param callable $callback
166
     * @return $this
167
     */
168
    public function action($action, callable $callback)
169
    {
170
171
         extract($this->splitAction($action));
0 ignored issues
show
Bug introduced by
$this->splitAction($action) cannot be passed to extract() as the parameter $var_array expects a reference.
Loading history...
172
173
         $action  = $this->setAction($action);
174
         $this->setHandler($handler);
175
176
        $action->injectionKernel($this);
177
        call_user_func($callback, $action);
178
        $this->currentAction = $action;
179
180
        return $this;
181
    }
182
183
    /**
184
     * @return array url -> string , http_type -> string
185
     *
186
     */
187
    public function get()
188
    {
189
        // here query in BPM
190
        return $this->currentAction->processData();
191
    }
192
193
    /**
194
     * Set collection for correct query
195
     * @param string $collection
196
     * @return mixed
197
     * @throws \Exception
198
     */
199
    public function setCollection($collection)
200
    {
201
        try {
202
            Assertion::regex($collection, '/[A-z]+Collection$/');
203
        } catch(AssertionFailedException $e) {
204
           throw new \Exception("Expected word 'Collection' in parameter method setCollection received : " .  $e->getValue());
205
        }
206
207
        return $this->collection = $collection;
208
    }
209
210
    /**
211
     * @return string
212
     */
213
    public function getCollection()
214
    {
215
        return $this->collection;
216
    }
217
218
    /**
219
     * @return ClientInterface
220
     */
221
    public function getCurl()
222
    {
223
        return app()->make(ClientInterface::class);
224
    }
225
    /**
226
     * @param $action
227
     * @return array
228
     */
229
    private function splitAction($action)
230
    {
231
        $split = explode(':', $action);
232
233
        // verification values
234
        Assertion::between(count($split), 2, 2);
235
        Assertion::keyExists( $this->action, $split[0]);
236
        Assertion::keyExists( $this->handlers, $split[1]);
237
238
        return ['action' => $split[0], 'handler' => $split[1]];
239
    }
240
241
    private function bootstrapping()
242
    {
243
        foreach ($this->serviceProviders as $provider)
244
        {
245
            (new $provider)->register();
246
        }
247
    }
248
}