Completed
Push — master ( de59f1...27fd57 )
by Tyler
02:43
created

Deploy::log()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 3
eloc 7
nc 3
nop 2
1
<?php
2
3
namespace Tylercd100\GitDeploy;
4
5
use Exception;
6
use Monolog\Logger;
7
8
class Deploy {
9
10
    /**
11
     * A Monolog Logger
12
     * 
13
     * @var Logger
14
     */
15
    protected $log;
16
17
    /**
18
     * The name of the branch to pull from.
19
     * 
20
     * @var string
21
     */
22
    protected $branch = 'master';
23
24
    /**
25
     * The name of the remote to pull from.
26
     * 
27
     * @var string
28
     */
29
    protected $remote = 'origin';
30
31
    /**
32
     * The directory where your website and git repository are located, can be 
33
     * a relative or absolute path
34
     * 
35
     * @var string
36
     */
37
    protected $directory;
38
39
    /**
40
     * @param string      $directory The directory where your website and git repository are located
41
     * @param string      $branch    The branch to pull
42
     * @param string      $remote    The remote to use
43
     * @param Logger|null $log       The Monolog Logger instance to use
44
     */
45
    public function __construct($directory, $branch = 'master', $remote = 'origin', Logger $log = null)
0 ignored issues
show
Unused Code introduced by
The parameter $branch is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $remote is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
46
    {
47
        // Determine the directory path
48
        $this->directory = realpath($directory).DIRECTORY_SEPARATOR;
49
50
        // Logger
51
        if(!$log instanceof Logger)
52
        {
53
            $log = new Logger('Deployment');
54
        }
55
        $this->log = $log;
56
57
        $this->log->info('Attempting deployment...');
58
    }
59
60
    /**
61
     * Executes the necessary commands to deploy the website.
62
     */
63
    public function execute()
64
    {
65
        try
66
        {
67
            // Make sure we're in the right directory
68
            exec('cd '.$this->directory, $output);
69
            $this->log->info('Changing working directory... '.implode(' ', $output));
70
71
            // Discard any changes to tracked files since our last deploy
72
            exec('git reset --hard HEAD', $output);
73
            $this->log->info('Reseting repository... '.implode(' ', $output));
74
75
            // Update the local repository
76
            exec('git pull '.$this->remote.' '.$this->branch, $output);
77
            $this->log->info('Pulling in changes... '.implode(' ', $output));
78
79
            // Secure the .git directory
80
            exec('chmod -R og-rx .git');
81
            $this->log->info('Securing .git directory... ');
82
83
            $this->log->info('Deployment successful.');
84
        }
85
        catch (Exception $e)
86
        {
87
            $this->log->error($e);
88
        }
89
    }
90
}
91