Completed
Push — master ( da9ea2...cfe8fe )
by Rougin
08:08
created

InstallDoctrineCommand::execute()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 84
Code Lines 46

Duplication

Lines 12
Ratio 14.29 %

Code Coverage

Tests 50
CRAP Score 5

Importance

Changes 7
Bugs 3 Features 1
Metric Value
dl 12
loc 84
ccs 50
cts 50
cp 1
rs 8.35
c 7
b 3
f 1
cc 5
eloc 46
nc 12
nop 2
crap 5

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Rougin\Combustor\Commands;
4
5
use Symfony\Component\Console\Input\InputInterface;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
use Rougin\Combustor\Common\Tools;
9
use Rougin\Combustor\Common\Commands\InstallCommand;
10
11
/**
12
 * Install Doctrine Command
13
 *
14
 * Installs Doctrine for CodeIgniter
15
 * 
16
 * @package Combustor
17
 * @author  Rougin Royce Gutib <[email protected]>
18
 */
19
class InstallDoctrineCommand extends InstallCommand
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $library = 'doctrine';
25
26
    /**
27
     * Executes the command.
28
     * 
29
     * @param \Symfony\Component\Console\Input\InputInterface   $input
30
     * @param \Symfony\Component\Console\Output\OutputInterface $output
31
     * @return object|\Symfony\Component\Console\Output\OutputInterface
32
     */
33 12
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35 12
        system('composer require doctrine/orm');
36
37 12
        $cli = $this->renderer->render('DoctrineCLI.template');
38 12
        $library = $this->renderer->render('Libraries/Doctrine.template');
39
40
        /**
41
         * Modifies the contents of vendor/bin/doctrine.php, creates the Doctrine
42
         * library and creates a "proxies" directory for lazy loading.
43
         */
44
45 12
        file_put_contents(realpath('vendor') . '/bin/doctrine.php', $cli);
46 12
        file_put_contents(realpath('vendor') . '/doctrine/orm/bin/doctrine.php', $cli);
47
48 12
        $file = fopen(APPPATH . 'libraries/Doctrine.php', 'wb');
49 12
        file_put_contents(APPPATH . 'libraries/Doctrine.php', $library);
50 12
        fclose($file);
51
52 12
        $autoload = file_get_contents(APPPATH . 'config/autoload.php');
53 12
        $lines = explode(PHP_EOL, $autoload);
54
55 12
        $pattern = '/\$autoload\[\'libraries\'\] = array\((.*?)\)/';
56
57 12
        preg_match_all($pattern, $lines[60], $match);
58
59 12
        $libraries = explode(', ', end($match[1]));
60
61 12 View Code Duplication
        if ( ! in_array('\'doctrine\'', $libraries)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62 12
            array_push($libraries, '\'doctrine\'');
63
64 12
            $libraries = array_filter($libraries);
65
66 12
            $pattern = '/\$autoload\[\'libraries\'\] = array\([^)]*\);/';
67 12
            $replacement = '$autoload[\'libraries\'] = array(' . implode(', ', $libraries) . ');';
68
69 12
            $lines[60] = preg_replace($pattern, $replacement, $lines[60]);
70
71 12
            file_put_contents(APPPATH . 'config/autoload.php', implode(PHP_EOL, $lines));
72 12
        }
73
74 12
        if ( ! is_dir(APPPATH . 'models/proxies')) {
75 12
            mkdir(APPPATH . 'models/proxies');
76 12
            chmod(APPPATH . 'models/proxies', 0777);
77 12
        }
78
79
        /*
80
         * Include the Base Model class in Doctrine CLI
81
         */
82
83 12
        $abstractCommand = file_get_contents(
84 12
            realpath('vendor') . '/' .
85 12
            'doctrine/orm/lib/Doctrine/'.
86 12
            'ORM/Tools/Console/Command/'.
87
            'SchemaTool/AbstractCommand.php'
88 12
        );
89
90 12
        $search  = 'use Doctrine\ORM\Tools\SchemaTool;';
91 12
        $replace = $search."\n".'include BASEPATH . \'core/Model.php\';';
92
93 12
        $contents = $abstractCommand;
94 12
        $schemaTool = 'use Doctrine\ORM\Tools\SchemaTool;';
95 12
        $coreModel = 'include BASEPATH . \'core/Model.php\';';
96
97 12
        if (strpos($abstractCommand, $schemaTool) !== false) {
98 12
            if (strpos($abstractCommand, $coreModel) === false) {
99 3
                $contents = str_replace($search, $replace, $abstractCommand);
100 3
            }
101 12
        }
102
103 12
        file_put_contents(
104 12
            realpath('vendor') . '/' .
105 12
            'doctrine/orm/lib/Doctrine/'.
106 12
            'ORM/Tools/Console/Command/'.
107 12
            'SchemaTool/AbstractCommand.php',
108
            $contents
109 12
        );
110
111 12
        Tools::ignite();
112
113 12
        $message = 'Doctrine ORM is now installed successfully!';
114
115 12
        return $output->writeln('<info>' . $message . '</info>');
116
    }
117
}
118