Completed
Push — master ( 1946fd...374d37 )
by Simon
02:16
created

Unsplash::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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
 * - connect to the server.
11
 * - list photos
12
 * @codeCoverageIgnore
13
 */
14
class Unsplash
15
{
16
17
    /**
18
     * Unsplash application ID from https://unsplash.com/developers
19
     * @var string
20
     */
21
    private $applicationId = '797a14e918f07f3559643a10f7c9e0de9d8a94262cd0ea0eb4b12c6d0993ed50';
22
23
    /**
24
     * Unsplash constructor (start HttpClient)
25
     */
26
    public function __construct()
27
    {
28
        HttpClient::init([
29
            'applicationId' => $this->applicationId,
30
        ]);
31
    }
32
33
    /**
34
     * Request APi to get last photos
35
     * @param  int $quantity Number of photos to return
36
     * @return string[] Photo download links indexed by IDs
37
     */
38
    public function allPhotos($quantity)
39
    {
40
        $photos = [];
41
42
        foreach (Photo::all(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 photos in category
51
     * @param  int $quantity Number of photos to return
52
     * @param  integer $category Category ID
53
     * @return string[] Photo download links indexed by IDs
54
     */
55
    public function photosInCategory($quantity, $category)
56
    {
57
        $photos = [];
58
59
        foreach (Category::find($category)->photos(1, $quantity) as $photo) {
60
            $photos[$photo->id] = $photo->links['download'];
61
        };
62
63
        return $photos;
64
    }
65
66
    /**
67
     * Request APi to get last featured photos
68
     * @param  int $quantity Number of photos to return
69
     * @return string[] Photo download links indexed by ID
70
     */
71
    public function featuredPhotos($quantity)
72
    {
73
        $photos = [];
74
75
        // process currated batches
76
        foreach (CuratedBatch::all(1, 100) as $batchInfo) {
77
            $batch = CuratedBatch::find($batchInfo->id);
78
79
            // process photos
80
            foreach ($batch->photos() as $photo) {
81
                $photos[$photo->id] = $photo->links['download'];
82
83
                // quit if $quantity photos have been found
84
                if (count($photos) >= $quantity) {
85
                    break 2;
86
                }
87
            }
88
        }
89
90
        return $photos;
91
    }
92
93
    /**
94
     * Request APi to get all categories photos
95
     * @return string[] Category names indexed by IDs
96
     */
97
    public function allCategories()
98
    {
99
        $categories = [];
100
101
        foreach (Category::all() as $category) {
102
            $categories[$category->id] = $category->title;
103
        };
104
105
        return $categories;
106
    }
107
}
108