-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathBrowseCatsPage.php
137 lines (122 loc) · 3.36 KB
/
BrowseCatsPage.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace Drupal\cat_api\Controller;
use Drupal\cat_api\Service\CatApiClientInterface;
use Drupal\cat_api\Service\CatBreedFactory;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Link;
use Drupal\Core\Render\Markup;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Class SearchPage.
*
* This demonstrates Dependency Injection in a Controller.
*
* @package Drupal\cat_api\Controller
*/
class BrowseCatsPage implements ContainerInjectionInterface {
/**
* Cat Api Client.
*
* @var \Drupal\cat_api\Service\CatApiClientInterface
*/
private $catApiClient;
/**
* Cat breed factory service.
*
* @var \Drupal\cat_api\Service\CatBreedFactory
*/
private $catBreedFactory;
/**
* Current request.
*
* @var \Symfony\Component\HttpFoundation\Request|null
*/
private $request;
/**
* SearchPage constructor.
*
* @param \Drupal\cat_api\Service\CatApiClientInterface $cat_api_client
* @param \Drupal\cat_api\Service\CatBreedFactory $cat_breed_factory
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
*/
public function __construct(CatApiClientInterface $cat_api_client, CatBreedFactory $cat_breed_factory, RequestStack $request_stack) {
$this->catApiClient = $cat_api_client;
$this->catBreedFactory = $cat_breed_factory;
$this->request = $request_stack->getCurrentRequest();
}
/**
* {@inheritDoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('cat_api.client'),
$container->get('cat_api.breed_factory'),
$container->get('request_stack')
);
}
/**
* Handle the cat browser page route.
*
* @param null $breed_id
*
* @return array
*/
public function page($breed_id = NULL) {
if (empty($breed_id)) {
return $this->getBreedLinks();
}
return [
'back' => [
'#markup' => Link::createFromRoute('Back', 'cat_api.browse_cats_page')->toString(),
],
'details' => $this->getBreedDetails($breed_id),
];
}
/**
* Get a list of links for all breeds.
*
* @return array
*/
private function getBreedLinks() {
$breeds = $this->catApiClient->get('breeds');
$links = [];
foreach ($breeds as $breed) {
$links[] = Link::createFromRoute($breed['name'], 'cat_api.browse_cats_page', ['breed_id' => $breed['id']]);
}
return [
'#theme' => 'item_list',
'#items' => $links,
];
}
/**
* Get details about a specific cat breed.
*
* @param string $breed_id
*
* @return array[]
*/
private function getBreedDetails(string $breed_id) {
$results = $this->catApiClient->get('images/search', [
'breed_ids' => $breed_id,
]);
$cat_breed = $this->catBreedFactory->createFromSearchResult($results[0]);
return [
'name' => [
'#type' => 'html_tag',
'#tag' => 'h2',
'#value' => $cat_breed->getName(),
],
'details' => [
'#theme' => 'item_list',
'#items' => [
Markup::create("<strong>Temperament</strong>: {$cat_breed->getTemperament()}"),
Markup::create("<strong>Description</strong>: {$cat_breed->getDescription()}"),
],
],
'image' => [
'#markup' => $cat_breed->getPicture(),
],
];
}
}