Skip to content

Commit 030b9e8

Browse files
committed
Changes logic for directory contents to add timestamp to all entries.
1 parent 4490557 commit 030b9e8

2 files changed

Lines changed: 333 additions & 355 deletions

File tree

src/Api.php

Lines changed: 56 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Api implements \Potherca\Flysystem\Github\ApiInterface
4242
const GITHUB_API_URL = 'https://api.github.com';
4343
const GITHUB_URL = 'https://github.com';
4444

45-
const MIME_TYPE_DIRECTORY = 'directory';
45+
const MIME_TYPE_DIRECTORY = 'directory'; // or application/x-directory
4646

4747
const NOT_RECURSIVE = false;
4848
const RECURSIVE = true;
@@ -191,15 +191,7 @@ final public function getFileContents($path)
191191
*/
192192
final public function getLastUpdatedTimestamp($path)
193193
{
194-
$path = $this->normalizePathName($path);
195-
196-
$commits = $this->commitsForFile($path);
197-
198-
$updated = array_shift($commits);
199-
200-
$time = new \DateTime($updated['commit']['committer']['date']);
201-
202-
return ['timestamp' => $time->getTimestamp()];
194+
return $this->filterCommits($path, 'reset');
203195
}
204196

205197
/**
@@ -211,15 +203,7 @@ final public function getLastUpdatedTimestamp($path)
211203
*/
212204
final public function getCreatedTimestamp($path)
213205
{
214-
$path = $this->normalizePathName($path);
215-
216-
$commits = $this->commitsForFile($path);
217-
218-
$created = array_pop($commits);
219-
220-
$time = new \DateTime($created['commit']['committer']['date']);
221-
222-
return ['timestamp' => $time->getTimestamp()];
206+
return $this->filterCommits($path, 'end');
223207
}
224208

225209
/**
@@ -254,7 +238,7 @@ final public function getMetaData($path)
254238
if ($this->isMetadataForDirectory($metadata) === true) {
255239
$metadata = $this->metadataForDirectory($path);
256240
}
257-
241+
258242
$this->metadata[$path] = $metadata;
259243
}
260244

@@ -284,7 +268,9 @@ final public function getDirectoryContents($path, $recursive)
284268
self::RECURSIVE //@NOTE: To retrieve all needed date the 'recursive' flag should always be 'true'
285269
);
286270

287-
$filteredTreeData = $this->filterTreeData($info[self::KEY_TREE], $path, $recursive);
271+
$treeData = $this->addTimestamps($info[self::KEY_TREE]);
272+
273+
$filteredTreeData = $this->filterTreeData($treeData, $path, $recursive);
288274

289275
return $this->normalizeTreeData($filteredTreeData);
290276
}
@@ -307,7 +293,7 @@ final public function guessMimeType($path)
307293

308294
/** @noinspection OffsetOperationsInspection *//* @NOTE: The existence of $meta[self::KEY_TYPE] has been validated by `hasKey`. */
309295
if ($this->hasKey($meta, self::KEY_TYPE) && $meta[self::KEY_TYPE] === self::KEY_DIRECTORY) {
310-
$mimeType = self::MIME_TYPE_DIRECTORY; // or application/x-directory
296+
$mimeType = self::MIME_TYPE_DIRECTORY;
311297
} else {
312298
$content = $this->getFileContents($path);
313299
$mimeType = MimeType::detectByContent($content);
@@ -356,13 +342,11 @@ private function filterTreeData(array $tree, $path, $recursive)
356342
$metadata = array_filter($tree, function ($entry) use ($path, $recursive, $length) {
357343
$match = false;
358344

359-
$entryPath = $entry[self::KEY_PATH];
360-
361-
if ($path === '' || strpos($entryPath, $path) === 0) {
345+
if ($path === '' || strpos($entry[self::KEY_PATH], $path) === 0) {
362346
if ($recursive === self::RECURSIVE) {
363347
$match = true;
364348
} else {
365-
$match = ($path !== '' || strpos($entryPath, '/', $length) === false);
349+
$match = ($path !== '' || strpos($entry[self::KEY_PATH], '/', $length) === false);
366350
}
367351
}
368352

@@ -396,8 +380,6 @@ private function guessVisibility($permissions)
396380
*/
397381
private function normalizeTreeData($treeData)
398382
{
399-
$normalizedTreeData = [];
400-
401383
if (is_array(current($treeData)) === false) {
402384
$treeData = [$treeData];
403385
}
@@ -409,20 +391,10 @@ private function normalizeTreeData($treeData)
409391

410392
$this->setDefaultValue($entry, self::KEY_CONTENTS);
411393
$this->setDefaultValue($entry, self::KEY_STREAM);
412-
$this->setDefaultValue($entry, self::KEY_TIMESTAMP);
413394

414395
return $entry;
415396
}, $treeData);
416397

417-
/* Add directory timestamp */
418-
$normalizedTreeData = array_map(function ($entry) use ($normalizedTreeData) {
419-
if ($entry[self::KEY_TYPE] === self::KEY_DIRECTORY) {
420-
$directoryTimestamp = $this->getDirectoryTimestamp($normalizedTreeData, $entry[self::KEY_PATH]);
421-
$entry[self::KEY_TIMESTAMP] = $directoryTimestamp;
422-
}
423-
return $entry;
424-
}, $normalizedTreeData);
425-
426398
return $normalizedTreeData;
427399
}
428400

@@ -435,7 +407,7 @@ private function normalizeTreeData($treeData)
435407
*/
436408
private function commitsForFile($path)
437409
{
438-
if (array_key_exists($path, $this->commits) === false) {
410+
if ($this->hasKey($this->commits, $path) === false) {
439411
$this->commits[$path] = $this->getCommitsApi()->all(
440412
$this->settings->getVendor(),
441413
$this->settings->getPackage(),
@@ -477,7 +449,10 @@ private function setEntryType(&$entry)
477449
case self::KEY_TREE:
478450
$entry[self::KEY_TYPE] = self::KEY_DIRECTORY;
479451
break;
452+
//@CHECKME: what should the 'default' be? Throw exception for unknown?
480453
}
454+
} else {
455+
$entry[self::KEY_TYPE] = false;
481456
}
482457
}
483458

@@ -561,10 +536,10 @@ private function getDirectoryTimestamp(array $treeMetadata, $path)
561536
$filteredTreeData = $this->filterTreeData($treeMetadata, $path, self::RECURSIVE);
562537

563538
array_walk($filteredTreeData, function ($entry) use (&$directoryTimestamp, $path) {
564-
if ($entry[self::KEY_TYPE] !== self::KEY_DIRECTORY
565-
&& $entry[self::KEY_TIMESTAMP] !== false
539+
if ($entry[self::KEY_TYPE] === self::KEY_FILE
566540
&& strpos($entry[self::KEY_PATH], $path) === 0
567541
) {
542+
// @CHECKME: Should the directory Timestamp reflect the `getCreatedTimestamp` or `getLastUpdatedTimestamp`?
568543
$timestamp = $this->getCreatedTimestamp($entry[self::KEY_PATH])[self::KEY_TIMESTAMP];
569544

570545
if ($timestamp > $directoryTimestamp) {
@@ -630,4 +605,44 @@ private function metadataForDirectory($path)
630605

631606
return $metadata;
632607
}
608+
609+
/**
610+
* @param array $treeData
611+
*
612+
* @return array
613+
*
614+
* @throws \Github\Exception\InvalidArgumentException
615+
*/
616+
private function addTimestamps(array $treeData)
617+
{
618+
return array_map(function ($entry) use ($treeData) {
619+
if ($entry[self::KEY_TYPE] === self::KEY_DIRECTORY) {
620+
$timestamp = $this->getDirectoryTimestamp($treeData, $entry[self::KEY_PATH]);
621+
} else {
622+
// @CHECKME: Should the Timestamp reflect the `getCreatedTimestamp` or `getLastUpdatedTimestamp`?
623+
$timestamp = $this->getCreatedTimestamp($entry[self::KEY_PATH])[self::KEY_TIMESTAMP];
624+
}
625+
$entry[self::KEY_TIMESTAMP] = $timestamp;
626+
627+
return $entry;
628+
}, $treeData);
629+
}
630+
631+
/**
632+
* @param $path
633+
* @param $function
634+
* @return array
635+
*/
636+
private function filterCommits($path, callable $function)
637+
{
638+
$path = $this->normalizePathName($path);
639+
640+
$commits = $this->commitsForFile($path);
641+
642+
$subject = $function($commits);
643+
644+
$time = new \DateTime($subject['commit']['committer']['date']);
645+
646+
return [self::KEY_TIMESTAMP => $time->getTimestamp()];
647+
}
633648
}

0 commit comments

Comments
 (0)