Completed
Push — master ( 2590ba...edce93 )
by Tyler
03:49 queued 01:16
created

Deploy::execute()   B

Complexity

Conditions 3
Paths 14

Size

Total Lines 32
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
rs 8.8571
cc 3
eloc 15
nc 14
nop 0
1
<?php
2
3
namespace Tylercd100\GitDeploy;
4
5
class Deploy {
6
7
    /**
8
     * A callback function to call after the deploy has finished.
9
     * 
10
     * @var callback
11
     */
12
    public $post_deploy;
13
14
    /**
15
     * The name of the file that will be used for logging deployments. Set to 
16
     * FALSE to disable logging.
17
     * 
18
     * @var string
19
     */
20
    private $_log = 'deployments.log';
21
22
    /**
23
     * The timestamp format used for logging.
24
     * 
25
     * @link    http://www.php.net/manual/en/function.date.php
26
     * @var     string
27
     */
28
    private $_date_format = 'Y-m-d H:i:sP';
29
30
    /**
31
     * The name of the branch to pull from.
32
     * 
33
     * @var string
34
     */
35
    private $_branch = 'master';
36
37
    /**
38
     * The name of the remote to pull from.
39
     * 
40
     * @var string
41
     */
42
    private $_remote = 'origin';
43
44
    /**
45
     * The directory where your website and git repository are located, can be 
46
     * a relative or absolute path
47
     * 
48
     * @var string
49
     */
50
    private $_directory;
51
52
    /**
53
     * Sets up defaults.
54
     * 
55
     * @param  string  $directory  Directory where your website is located
56
     * @param  array   $data       Information about the deployment
0 ignored issues
show
Bug introduced by
There is no parameter named $data. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
57
     */
58
    public function __construct($directory, $options = array())
59
    {
60
        // Determine the directory path
61
        $this->_directory = realpath($directory).DIRECTORY_SEPARATOR;
62
63
        $available_options = array('log', 'date_format', 'branch', 'remote');
64
65
        foreach ($options as $option => $value)
66
        {
67
            if (in_array($option, $available_options))
68
            {
69
                $this->{'_'.$option} = $value;
70
            }
71
        }
72
73
        $this->log('Attempting deployment...');
74
    }
75
76
    /**
77
     * Writes a message to the log file.
78
     * 
79
     * @param  string  $message  The message to write
80
     * @param  string  $type     The type of log message (e.g. INFO, DEBUG, ERROR, etc.)
81
     */
82
    public function log($message, $type = 'INFO')
83
    {
84
        if ($this->_log)
85
        {
86
            // Set the name of the log file
87
            $filename = $this->_log;
88
89
            if ( ! file_exists($filename))
90
            {
91
                // Create the log file
92
                file_put_contents($filename, '');
93
94
                // Allow anyone to write to log files
95
                chmod($filename, 0666);
96
            }
97
98
            // Write the message into the log file
99
            // Format: time --- type: message
100
            file_put_contents($filename, date($this->_date_format).' --- '.$type.': '.$message.PHP_EOL, FILE_APPEND);
101
        }
102
    }
103
104
    /**
105
     * Executes the necessary commands to deploy the website.
106
     */
107
    public function execute()
108
    {
109
        try
110
        {
111
            // Make sure we're in the right directory
112
            exec('cd '.$this->_directory, $output);
113
            $this->log('Changing working directory... '.implode(' ', $output));
114
115
            // Discard any changes to tracked files since our last deploy
116
            exec('git reset --hard HEAD', $output);
117
            $this->log('Reseting repository... '.implode(' ', $output));
118
119
            // Update the local repository
120
            exec('git pull '.$this->_remote.' '.$this->_branch, $output);
121
            $this->log('Pulling in changes... '.implode(' ', $output));
122
123
            // Secure the .git directory
124
            exec('chmod -R og-rx .git');
125
            $this->log('Securing .git directory... ');
126
127
            if (is_callable($this->post_deploy))
128
            {
129
                call_user_func($this->post_deploy, $this->_data);
0 ignored issues
show
Bug introduced by
The property _data does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
130
            }
131
132
            $this->log('Deployment successful.');
133
        }
134
        catch (Exception $e)
0 ignored issues
show
Bug introduced by
The class Tylercd100\GitDeploy\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
135
        {
136
            $this->log($e, 'ERROR');
137
        }
138
    }
139
}
140