Completed
Push — master ( bd8b26...543084 )
by Zach
01:43
created

Yarak   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 65
rs 10
wmc 5
lcom 0
cbo 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A call() 0 16 2
A getKernelWithConfig() 0 4 1
A getKernel() 0 19 2
1
<?php
2
3
namespace Yarak;
4
5
use Phalcon\Di\FactoryDefault;
6
use Yarak\Exceptions\FileNotFound;
7
use Symfony\Component\Console\Input\ArrayInput;
8
use Symfony\Component\Console\Output\NullOutput;
9
10
class Yarak
11
{
12
    /**
13
     * Call a Yarak console command.
14
     *
15
     * @param string $command
16
     * @param array  $arguments Argument array.
17
     * @param array  $config    Config values, for testing purposes.
18
     */
19
    public static function call($command, array $arguments = [], array $config = [])
20
    {
21
        if (!empty($config)) {
22
            $kernel = self::getKernelWithConfig($config);
23
        } else {
24
            $kernel = self::getKernel();
25
        }
26
27
        $arguments = ['command' => $command] + $arguments;
28
29
        $input = new ArrayInput($arguments);
30
31
        $output = new NullOutput();
32
33
        $kernel->handle($input, $output);
34
    }
35
36
    /**
37
     * Get an instance of Yarak kernel built with the given config.
38
     *
39
     * @param array $config
40
     *
41
     * @return Kernel
42
     */
43
    protected static function getKernelWithConfig(array $config)
44
    {
45
        return new Kernel($config);
46
    }
47
48
    /**
49
     * Resolve Yarak kernel from di.
50
     *
51
     * @throws FileNotFound
52
     *
53
     * @return Kernel
54
     */
55
    protected static function getKernel()
56
    {
57
        $di = new FactoryDefault();
58
59
        $servicesPath = __DIR__.'/../../../../app/config/services.php';
60
61
        try {
62
            include $servicesPath;
63
        } catch (\Exception $e) {
64
            throw FileNotFound::servicesFileNotFound(
65
                'Try passing the Yarak config array as the third argument '.
66
                'to Yarak::call.'
67
            );
68
        }
69
70
        $di->getConfig();
71
72
        return $di->get('yarak');
73
    }
74
}
75