Completed
Push — master ( 53438b...04741d )
by Tyler
03:16 queued 01:15
created

Deploy::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 5
Bugs 1 Features 3
Metric Value
c 5
b 1
f 3
dl 0
loc 14
ccs 10
cts 10
cp 1
rs 9.4285
cc 2
eloc 7
nc 2
nop 4
crap 2
1
<?php
2
3
namespace Tylercd100\GitDeploy;
4
5
use Exception;
6
use Monolog\Logger;
7
use Psr\Log\LoggerInterface;
8
use Psr\Log\LoggerAwareInterface;
9
use Psr\Log\LoggerAwareTrait;
10
11
class Deploy implements LoggerAwareInterface{
12
13
    use LoggerAwareTrait;
14
15
    /**
16
     * The name of the branch to pull from.
17
     * 
18
     * @var string
19
     */
20
    protected $branch = 'master';
21
22
    /**
23
     * The name of the remote to pull from.
24
     * 
25
     * @var string
26
     */
27
    protected $remote = 'origin';
28
29
    /**
30
     * The directory where your website and git repository are located, can be 
31
     * a relative or absolute path
32
     * 
33
     * @var string
34
     */
35
    protected $directory;
36
37
    /**
38
     * @param string               $directory The directory where your website and git repository are located
39
     * @param string               $branch The branch to pull
40
     * @param string               $remote The remote to use
41
     * @param LoggerInterface|null $logger The LoggerInterface instance to use
42
     */
43 3
    public function __construct($directory, $branch = 'master', $remote = 'origin', LoggerInterface $logger = null)
44
    {
45
        // LoggerInterface
46 3
        if(!$logger instanceof LoggerInterface)
47 3
        {
48 3
            $logger = new Logger('Deployment');
49 3
        }
50 3
        $this->setLogger($logger);
51
52
        //Properties
53 3
        $this->directory = realpath($directory).DIRECTORY_SEPARATOR;
54 3
        $this->branch = $branch;
55 3
        $this->remote = $remote;
56 3
    }
57
58
    /**
59
     * Executes the necessary commands to deploy the website.
60
     */
61
    public function execute()
62
    {
63
        $this->logger->debug('Attempting deployment...');
64
65
        try
66
        {
67
            // Make sure we're in the right directory
68
            exec('cd '.$this->directory, $output);
69
            $this->logger->debug('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->logger->debug('Reseting repository... '.implode(' ', $output));
74
75
            // Update the local repository
76
            exec('git pull '.$this->remote.' '.$this->branch, $output);
77
            $this->logger->debug('Pulling in changes... '.implode(' ', $output));
78
79
            // Secure the .git directory
80
            exec('chmod -R og-rx .git');
81
            $this->logger->debug('Securing .git directory... ');
82
83
            $this->logger->notice('Deployment successful.');
84
        }
85
        catch (Exception $e)
86
        {
87
            $this->logger->error($e);
88
        }
89
    }
90
}
91