Skip to content

Commit d71619f

Browse files
committed
Code cleanup for Client class.
1 parent fefccc5 commit d71619f

3 files changed

Lines changed: 173 additions & 97 deletions

File tree

src/Client.php

Lines changed: 99 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ class Client implements ClientInterface
1818
const API_REPO = 'repo';
1919

2020
const KEY_BLOB = 'blob';
21-
const KEY_CONTENTS = 'contents';
2221
const KEY_DIRECTORY = 'dir';
2322
const KEY_FILE = 'file';
2423
const KEY_FILENAME = 'basename';
@@ -32,10 +31,11 @@ class Client implements ClientInterface
3231
const KEY_TREE = 'tree';
3332
const KEY_TYPE = 'type';
3433
const KEY_VISIBILITY = 'visibility';
34+
const ERROR_NO_NAME = 'Could not set name for entry';
3535

3636
/** @var GithubClient */
3737
private $client;
38-
/** @var Settings */
38+
/** @var SettingsInterface */
3939
private $settings;
4040
/** @var bool */
4141
private $isAuthenticationAttempted = false;
@@ -125,24 +125,31 @@ final public function getFileContents($path)
125125
*/
126126
final public function getLastUpdatedTimestamp($path)
127127
{
128-
// List commits for a file
129-
$commits = $this->getRepositoryApi()->commits()->all(
130-
$this->settings->getVendor(),
131-
$this->settings->getPackage(),
132-
array(
133-
'sha' => $this->settings->getBranch(),
134-
'path' => $path
135-
)
136-
);
128+
$commits = $this->commitsForFile($path);
137129

138130
$updated = array_shift($commits);
139-
//@NOTE: $created = array_pop($commits);
140131

141132
$time = new \DateTime($updated['commit']['committer']['date']);
142133

143134
return ['timestamp' => $time->getTimestamp()];
144135
}
145136

137+
/**
138+
* @param string $path
139+
*
140+
* @return array
141+
*/
142+
final public function getCreatedTimestamp($path)
143+
{
144+
$commits = $this->commitsForFile($path);
145+
146+
$created = array_pop($commits);
147+
148+
$time = new \DateTime($created['commit']['committer']['date']);
149+
150+
return ['timestamp' => $time->getTimestamp()];
151+
}
152+
146153
/**
147154
* @param string $path
148155
*
@@ -294,49 +301,95 @@ private function normalizeTreeMetadata($metadata)
294301
}
295302

296303
foreach ($metadata as $entry) {
297-
if (isset($entry[self::KEY_NAME]) === false){
298-
if(isset($entry[self::KEY_FILENAME]) === true) {
299-
$entry[self::KEY_NAME] = $entry[self::KEY_FILENAME];
300-
} elseif(isset($entry[self::KEY_PATH]) === true) {
301-
$entry[self::KEY_NAME] = $entry[self::KEY_PATH];
302-
} else {
303-
// ?
304-
}
305-
}
304+
$this->setEntryName($entry);
305+
$this->setEntryType($entry);
306+
$this->setEntryVisibility($entry);
306307

307-
if (isset($entry[self::KEY_TYPE]) === true) {
308-
switch ($entry[self::KEY_TYPE]) {
309-
case self::KEY_BLOB:
310-
$entry[self::KEY_TYPE] = self::KEY_FILE;
311-
break;
308+
$this->setDefaultValue($entry, self::KEY_CONTENTS);
309+
$this->setDefaultValue($entry, self::KEY_STREAM);
310+
$this->setDefaultValue($entry, self::KEY_TIMESTAMP);
312311

313-
case self::KEY_TREE:
314-
$entry[self::KEY_TYPE] = self::KEY_DIRECTORY;
315-
break;
316-
}
317-
}
318312

319-
if (isset($entry[self::KEY_CONTENTS]) === false) {
320-
$entry[self::KEY_CONTENTS] = false;
321-
}
313+
$result[] = $entry;
314+
}
322315

323-
if (isset($entry[self::KEY_STREAM]) === false) {
324-
$entry[self::KEY_STREAM] = false;
325-
}
316+
return $result;
317+
}
326318

327-
if (isset($entry[self::KEY_TIMESTAMP]) === false) {
328-
$entry[self::KEY_TIMESTAMP] = false;
329-
}
319+
/**
320+
* @param $path
321+
*
322+
* @return array
323+
*/
324+
private function commitsForFile($path)
325+
{
326+
return $this->getRepositoryApi()->commits()->all(
327+
$this->settings->getVendor(),
328+
$this->settings->getPackage(),
329+
array(
330+
'sha' => $this->settings->getBranch(),
331+
'path' => $path
332+
)
333+
);
334+
}
330335

331-
if (isset($entry[self::KEY_MODE])) {
332-
$entry[self::KEY_VISIBILITY] = $this->guessVisibility($entry[self::KEY_MODE]);
333-
} else {
334-
$entry[self::KEY_VISIBILITY] = false;
336+
/**
337+
* @param array $entry
338+
* @param string $key
339+
* @param bool $default
340+
*
341+
* @return mixed
342+
*/
343+
private function setDefaultValue(array &$entry, $key, $default = false)
344+
{
345+
if (isset($entry[$key]) === false) {
346+
$entry[$key] = $default;
347+
}
348+
}
349+
350+
/**
351+
* @param $entry
352+
*/
353+
private function setEntryType(&$entry)
354+
{
355+
if (isset($entry[self::KEY_TYPE]) === true) {
356+
switch ($entry[self::KEY_TYPE]) {
357+
case self::KEY_BLOB:
358+
$entry[self::KEY_TYPE] = self::KEY_FILE;
359+
break;
360+
361+
case self::KEY_TREE:
362+
$entry[self::KEY_TYPE] = self::KEY_DIRECTORY;
363+
break;
335364
}
365+
}
366+
}
336367

337-
$result[] = $entry;
368+
/**
369+
* @param $entry
370+
*/
371+
private function setEntryVisibility(&$entry)
372+
{
373+
if (isset($entry[self::KEY_MODE])) {
374+
$entry[self::KEY_VISIBILITY] = $this->guessVisibility($entry[self::KEY_MODE]);
375+
} else {
376+
$entry[self::KEY_VISIBILITY] = false;
338377
}
378+
}
339379

340-
return $result;
380+
/**
381+
* @param $entry
382+
*/
383+
private function setEntryName(&$entry)
384+
{
385+
if (isset($entry[self::KEY_NAME]) === false) {
386+
if (isset($entry[self::KEY_FILENAME]) === true) {
387+
$entry[self::KEY_NAME] = $entry[self::KEY_FILENAME];
388+
} elseif (isset($entry[self::KEY_PATH]) === true) {
389+
$entry[self::KEY_NAME] = $entry[self::KEY_PATH];
390+
} else {
391+
$entry[self::KEY_NAME] = null;
392+
}
393+
}
341394
}
342395
}

src/ClientInterface.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
interface ClientInterface
1111
{
12+
const KEY_CONTENTS = 'contents';
13+
1214
/**
1315
* @param string $path
1416
*

0 commit comments

Comments
 (0)