Completed
Pull Request — experimental/3.1 (#2588)
by Ryo
139:43 queued 96:01
created

ComposerProcessService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 98
Duplicated Lines 36.73 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 5
Bugs 0 Features 0
Metric Value
dl 36
loc 98
rs 10
c 5
b 0
f 0
wmc 6
lcom 1
cbo 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPHP() 0 4 1
A execRequire() 18 18 1
A execRemove() 18 18 1
A setupComposer() 0 21 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
namespace Eccube\Service;
24
25
use Eccube\Annotation\Inject;
26
use Eccube\Annotation\Service;
27
use Eccube\Application;
28
29
/**
30
 * Class ComposerProcessService
31
 * @package Eccube\Service
32
 * @Service
33
 */
34
class ComposerProcessService
35
{
36
    /**
37
     * @Inject("config")
38
     * @var array
39
     */
40
    protected $appConfig;
41
42
    private $composerFile;
43
    private $composerSetup;
44
    private static $vendorName = 'ec-cube';
45
46
    /**
47
     * This function to install a plugin by composer require
48
     *
49
     * @param string $packageName
50
     * @return bool
51
     */
52 View Code Duplication
    public function execRequire($packageName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
53
    {
54
        set_time_limit(0);
55
        $this->setupComposer();
56
        // Build command
57
        $packageName = self::$vendorName.'/'.$packageName;
58
        $command = $this->getPHP().' '.$this->composerFile.' require '.$packageName;
59
        $command .= ' --prefer-dist --no-progress --no-suggest --no-scripts --ignore-platform-reqs --profile --no-ansi --no-interaction -d ';
60
        $command .= $this->appConfig['root_dir'].' 2>&1';
61
        log_info($command);
62
63
        // Execute command
64
        $output = array();
65
        exec($command, $output);
66
        log_info(PHP_EOL.implode(PHP_EOL, $output).PHP_EOL);
67
68
        return true;
69
    }
70
71
    /**
72
     * This function to remove a plugin by composer remove
73
     * Note: Remove with dependency, if not, please add " --no-update-with-dependencies"
74
     *
75
     * @param string $packageName
76
     * @return bool
77
     */
78 View Code Duplication
    public function execRemove($packageName)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
79
    {
80
        set_time_limit(0);
81
        $this->setupComposer();
82
        // Build command
83
        $packageName = self::$vendorName.'/'.$packageName;
84
        $command = $this->getPHP().' '.$this->composerFile.' remove '.$packageName;
85
        $command .= ' --no-progress --no-scripts --ignore-platform-reqs --profile --no-ansi --no-interaction -d ';
86
        $command .= $this->appConfig['root_dir'].' 2>&1';
87
        log_info($command);
88
89
        // Execute command
90
        $output = array();
91
        exec($command, $output);
92
        log_info(PHP_EOL.implode(PHP_EOL, $output).PHP_EOL);
93
94
        return true;
95
    }
96
97
    /**
98
     * Get environment php command
99
     *
100
     * @return string
101
     */
102
    private function getPHP()
103
    {
104
        return 'php';
105
    }
106
107
    /**
108
     * Check composer file and setup it
109
     */
110
    private function setupComposer()
111
    {
112
        @ini_set('memory_limit', '1536M');
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
113
        // Config for some environment
114
        putenv('COMPOSER_HOME='.$this->appConfig['plugin_realdir'].'/.composer');
115
        $this->composerFile = $this->appConfig['root_dir'].'/composer.phar';
116
        $this->composerSetup = $this->appConfig['root_dir'].'/composer-setup.php';
117
118
        if (!file_exists($this->composerFile)) {
119
            if (!file_exists($this->composerSetup)) {
120
                $result = copy('https://getcomposer.org/installer', $this->composerSetup);
121
                log_info($this->composerSetup.' : '.$result);
122
            }
123
            $command = $this->getPHP().' '.$this->composerSetup;
124
            $output = array();
125
            exec($command, $output);
126
            log_info(PHP_EOL.implode(PHP_EOL, $output).PHP_EOL);
127
128
            unlink($this->composerSetup);
129
        }
130
    }
131
}
132