DNGitFetch::log()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * Class DNGitFetch
5
 *
6
 * @property string $ResqueToken
7
 *
8
 * @method DNProject Project()
9
 * @property int $ProjectID
10
 * @method Member Deployer()
11
 * @property int $DeployerID
12
 *
13
 */
14
class DNGitFetch extends DataObject {
0 ignored issues
show
Coding Style introduced by
The property $has_one is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
15
16
	/**
17
	 * @var array
18
	 */
19
	private static $db = array(
20
		"ResqueToken" => "Varchar(255)",
21
		// Observe that this is not the same as Resque status, since ResqueStatus is not persistent
22
		// It's used for finding successful deployments and displaying that in history views in the frontend
23
		"Status" => "Enum('Queued, Started, Finished, Failed, n/a', 'n/a')",
24
	);
25
26
	/**
27
	 * @var array
28
	 */
29
	private static $has_one = array(
30
		"Project" => "DNProject",
31
		"Deployer" => "Member"
32
	);
33
34
	/**
35
	 * @param int $int
36
	 * @return string
37
	 */
38 View Code Duplication
	public static function map_resque_status($int) {
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...
39
		$remap = array(
40
			Resque_Job_Status::STATUS_WAITING => "Queued",
41
			Resque_Job_Status::STATUS_RUNNING => "Running",
42
			Resque_Job_Status::STATUS_FAILED => "Failed",
43
			Resque_Job_Status::STATUS_COMPLETE => "Complete",
44
			false => "Invalid",
45
		);
46
		return $remap[$int];
47
	}
48
49
	/**
50
	 * Queue a fetch job
51
	 * @param bool $forceClone Force repository to be re-cloned
52
	 */
53
	public function start($forceClone = false) {
54
		$project = $this->Project();
55
		$log = $this->log();
56
57
		if(!$this->DeployerID) {
58
			$this->DeployerID = Member::currentUserID();
59
		}
60
61 View Code Duplication
		if($this->DeployerID) {
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
			$deployer = $this->Deployer();
63
			$message = sprintf(
64
				'Update repository job for %s initiated by %s (%s)',
65
				$project->Name,
66
				$deployer->getName(),
67
				$deployer->Email
68
			);
69
			$log->write($message);
70
		}
71
72
		// write first, so we have the ID. We have to write again
73
		// later once we have the resque token.
74
		$this->write();
75
76
		$args = array(
77
			'projectID' => $project->ID,
78
			'logfile' => $this->logfile(),
79
			'fetchID' => $this->ID,
80
			'forceClone' => $forceClone
81
		);
82
83
		$token = Resque::enqueue('git', 'FetchJob', $args, true);
84
		$this->ResqueToken = $token;
85
		$this->write();
86
87
		$message = sprintf('Fetch queued as job %s', $token);
88
		$log->write($message);
89
	}
90
91
	/**
92
	 * @param Member|null $member
93
	 * @return bool
94
	 */
95
	public function canView($member = null) {
96
		return $this->Project()->canView($member);
97
	}
98
99
	/**
100
	 * Return a path to the log file.
101
	 * @return string
102
	 */
103
	protected function logfile() {
104
		return sprintf(
105
			'%s.fetch.%s.log',
106
			$this->Project()->Name,
107
			$this->ID
108
		);
109
	}
110
111
	/**
112
	 * @return \DeploynautLogFile
113
	 */
114
	public function log() {
115
		return new DeploynautLogFile($this->logfile());
116
	}
117
118
	/**
119
	 * @return string
120
	 */
121
	public function LogContent() {
122
		return $this->log()->content();
123
	}
124
125
	/**
126
	 * Returns the status of the resque job
127
	 *
128
	 * @return string
129
	 */
130
	public function ResqueStatus() {
131
		$status = new Resque_Job_Status($this->ResqueToken);
0 ignored issues
show
Security Bug introduced by
It seems like $this->ResqueToken can also be of type false; however, Resque_Job_Status::__construct() does only seem to accept string, did you maybe forget to handle an error condition?
Loading history...
132
		$statusCode = $status->get();
133
		// The Resque job can no longer be found, fallback to the DNDeployment.Status
134
		if($statusCode === false) {
135
			// Translate from the DNDeployment.Status to the Resque job status for UI purposes
136
			switch($this->Status) {
0 ignored issues
show
Documentation introduced by
The property Status does not exist on object<DNGitFetch>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
137
				case 'Finished':
138
					return 'Complete';
139
				case 'Started':
140
					return 'Running';
141
				default:
142
					return $this->Status;
0 ignored issues
show
Documentation introduced by
The property Status does not exist on object<DNGitFetch>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
143
			}
144
		}
145
		return self::map_resque_status($statusCode);
146
	}
147
148
}
149