Completed
Push — master ( c9c9d6...9f49ce )
by Zach
02:07
created

Yarak::getDI()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 0
dl 0
loc 21
rs 9.3142
c 0
b 0
f 0
1
<?php
2
3
namespace Yarak;
4
5
use Phalcon\DiInterface;
6
use Phalcon\Di\FactoryDefault;
7
use Yarak\Exceptions\FileNotFound;
8
use Symfony\Component\Console\Input\ArrayInput;
9
use Symfony\Component\Console\Output\NullOutput;
10
11
class Yarak
12
{
13
    /**
14
     * Call a Yarak console command.
15
     *
16
     * @param string         $command
17
     * @param array          $arguments Argument array.
18
     * @param FactoryDefault $di        DI, may be necessary for php 5.6.
19
     */
20
    public static function call($command, array $arguments = [], DiInterface $di = null)
21
    {
22
        $kernel = self::getKernel($di);
23
24
        $arguments = ['command' => $command] + $arguments;
25
26
        $input = new ArrayInput($arguments);
27
28
        $output = new NullOutput();
29
30
        $kernel->handle($input, $output);
31
    }
32
33
    /**
34
     * Resolve Yarak kernel from di.
35
     *
36
     * @param FactoryDefault|null $di
37
     *
38
     * @return Kernel
39
     */
40
    protected static function getKernel($di)
41
    {
42
        if ($di === null) {
43
            $di = self::getDI();
44
        }
45
46
        return $di->get('yarak');
47
    }
48
49
    /**
50
     * Get a fresh DI instance.
51
     *
52
     * @throws FileNotFound
53
     *
54
     * @return FactoryDefault
55
     */
56
    protected static function getDI()
57
    {
58
        $di = new FactoryDefault();
59
60
        $servicesPath = __DIR__.'/../../../../app/config/services.php';
61
62
        if (!realpath($servicesPath)) {
63
            $servicesPath = __DIR__.'/../app/config/services.php';
64
        }
65
66
        try {
67
            include $servicesPath;
68
        } catch (\Exception $e) {
69
            throw FileNotFound::servicesFileNotFound(
70
                'Try passing the Yarak config array as the third argument '.
71
                'to Yarak::call.'
72
            );
73
        }
74
75
        return $di;
76
    }
77
}
78