Completed
Push — master ( fe0371...1946fd )
by Simon
04:01
created

Unsplash::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 5
rs 9.4285
1
<?php namespace Simondubois\UnsplashDownloader;
2
3
use Crew\Unsplash\Category;
4
use Crew\Unsplash\CuratedBatch;
5
use Crew\Unsplash\HttpClient;
6
use Crew\Unsplash\Photo;
7
8
/**
9
 * A proxy to deal with the Unsplah API :
10
 * - list photos
11
 * @codeCoverageIgnore
12
 */
13
class Unsplash
14
{
15
16
    /**
17
     * Request APi to get last photos
18
     * @param  int $quantity Number of photos to return
19
     * @return string[] Photo download links indexed by IDs
20
     */
21
    public function allPhotos($quantity)
22
    {
23
        $photos = [];
24
25
        foreach (Photo::all(1, $quantity) as $photo) {
26
            $photos[$photo->id] = $photo->links['download'];
27
        };
28
29
        return $photos;
30
    }
31
32
    /**
33
     * Request APi to get last photos in category
34
     * @param  int $quantity Number of photos to return
35
     * @param  integer $category Category ID
36
     * @return string[] Photo download links indexed by IDs
37
     */
38
    public function photosInCategory($quantity, $category)
39
    {
40
        $photos = [];
41
42
        foreach (Category::find($category)->photos(1, $quantity) as $photo) {
43
            $photos[$photo->id] = $photo->links['download'];
44
        };
45
46
        return $photos;
47
    }
48
49
    /**
50
     * Request APi to get last featured photos
51
     * @param  int $quantity Number of photos to return
52
     * @return string[] Photo download links indexed by ID
53
     */
54
    public function featuredPhotos($quantity)
55
    {
56
        $photos = [];
57
58
        // process currated batches
59
        foreach (CuratedBatch::all(1, 100) as $batchInfo) {
60
            $batch = CuratedBatch::find($batchInfo->id);
61
62
            // process photos
63
            foreach ($batch->photos() as $photo) {
64
                $photos[$photo->id] = $photo->links['download'];
65
66
                // quit if $quantity photos have been found
67
                if (count($photos) >= $quantity) {
68
                    break 2;
69
                }
70
            }
71
        }
72
73
        return $photos;
74
    }
75
76
    /**
77
     * Request APi to get all categories photos
78
     * @return string[] Category names indexed by IDs
79
     */
80
    public function allCategories()
81
    {
82
        $categories = [];
83
84
        foreach (Category::all() as $category) {
85
            $categories[$category->id] = $category->title;
86
        };
87
88
        return $categories;
89
    }
90
}
91