1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Columnis\Service\Factory; |
4
|
|
|
|
5
|
|
|
use Zend\ServiceManager\FactoryInterface; |
6
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
7
|
|
|
use Zend\Cache\StorageFactory; |
8
|
|
|
use GuzzleHttp\Client as GuzzleClient; |
9
|
|
|
use GuzzleHttp\Subscriber\Cache\CacheSubscriber; |
10
|
|
|
use GuzzleHttp\Message\RequestInterface; |
11
|
|
|
use Columnis\Exception\Api\ClientNumberNotSetException; |
12
|
|
|
use Columnis\Exception\Api\ApiBaseUrlNotSetException; |
13
|
|
|
use Columnis\Service\ApiService; |
14
|
|
|
use Columnis\Model\ZfCacheAdapter; |
15
|
|
|
|
16
|
|
|
class ApiServiceFactory implements FactoryInterface { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* {@inheritDoc} |
20
|
|
|
* |
21
|
|
|
* @return ApiService |
22
|
|
|
*/ |
23
|
7 |
|
public function createService(ServiceLocatorInterface $serviceLocator) { |
24
|
7 |
|
$config = $serviceLocator->get('Config'); |
25
|
|
|
|
26
|
7 |
|
$columnisConfig = isset($config['columnis']) ? $config['columnis'] : array(); |
27
|
|
|
|
28
|
7 |
|
$apiConfig = isset($columnisConfig['api_settings']) ? $columnisConfig['api_settings'] : array(); |
29
|
|
|
|
30
|
7 |
|
if(!isset($apiConfig['client_number'])) { |
31
|
|
|
throw new ClientNumberNotSetException("There is no client_number set in local.php config file."); |
32
|
|
|
} |
33
|
7 |
|
if(!isset($apiConfig['api_base_url'])) { |
34
|
|
|
throw new ApiBaseUrlNotSetException("There is no api_base_url set in local.php config file."); |
35
|
|
|
} |
36
|
|
|
|
37
|
7 |
|
$clientNumber = $apiConfig['client_number']; |
38
|
7 |
|
$apiUrl = $apiConfig['api_base_url']; |
39
|
7 |
|
$httpClient = new GuzzleClient(array('base_url' => $apiUrl)); |
40
|
|
|
|
41
|
7 |
|
$cacheConfig = isset($config['guzzle_cache']) ? $config['guzzle_cache'] : array(); |
42
|
7 |
|
if(isset($cacheConfig['adapter'])) { |
43
|
|
|
$cache = StorageFactory::factory($cacheConfig); |
44
|
|
|
$zfCacheAdapter = new ZfCacheAdapter($cache); |
45
|
|
|
|
46
|
|
|
CacheSubscriber::attach( |
47
|
|
|
$httpClient, |
48
|
|
|
[ |
49
|
|
|
'storage' => $zfCacheAdapter, |
50
|
|
|
'can_cache' => function(RequestInterface $request) use ($zfCacheAdapter){ |
51
|
7 |
|
return !$request->hasHeader('Authorization'); |
52
|
|
|
} |
53
|
|
|
] |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return new ApiService($httpClient, $clientNumber); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|