Skip to content

Commit cd7e1dc

Browse files
author
Alex Semeniuc
committed
Case 2694 - Added statistics.
1 parent ef29b54 commit cd7e1dc

File tree

3 files changed

+62
-27
lines changed

3 files changed

+62
-27
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# IDE
2+
/.buildpath
3+
/.project
4+
/.settings
5+
6+
/composer.phar

Bpi/Sdk/Bpi.php

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
<?php
2-
32
require_once __DIR__ . '/../../vendor/autoload.php';
43

54
class Bpi
65
{
7-
protected $client;
8-
protected $authorization;
9-
protected $endpoint;
6+
protected $client;
7+
protected $authorization;
8+
protected $endpoint;
109

10+
/**
11+
* Create Bpi Client
12+
* @param string $endpoint URL
13+
* @param string $agency_id Agency ID
14+
* @param string $api_key App key
15+
* @param string $secret_key
16+
*/
1117
public function __construct($endpoint, $agency_id, $api_key, $secret_key)
1218
{
1319
$this->client = new \Goutte\Client();
@@ -45,4 +51,23 @@ public function searchNodes(array $queries = array())
4551
$nodes->reduceItemsByAttr('type', 'entity');
4652
return new \Bpi\Sdk\NodeList($nodes);
4753
}
54+
55+
/**
56+
* Get statistics
57+
* Parameterformat: Y-m-d
58+
* @param string $dateFrom
59+
* @param string $dateTo
60+
*/
61+
public function getStatistics($dateFrom, $dateTo)
62+
{
63+
$result = $this->createDocument();
64+
$endpoint = clone $this->endpoint;
65+
$endpoint->firstItem('name', 'node')
66+
->query('statistics')
67+
->send($result, array('dateFrom'=>$dateFrom, 'dateTo'=>$dateTo));
68+
69+
$item = new \Bpi\Sdk\Item\BaseItem($result);
70+
71+
return $item;
72+
}
4873
}

Bpi/Sdk/Document.php

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Document implements \Iterator, \Countable
1111
* @var \Goutte\Client
1212
*/
1313
protected $http_client;
14-
14+
1515
/**
1616
*
1717
* @var \Bpi\Sdk\Authorization
@@ -23,7 +23,7 @@ class Document implements \Iterator, \Countable
2323
* @var \Symfony\Component\DomCrawler\Crawler
2424
*/
2525
protected $crawler;
26-
26+
2727
/**
2828
*
2929
* @param \Goutte\Client $client
@@ -34,7 +34,7 @@ public function __construct(Client $client, Authorization $authorization)
3434
$this->http_client = $client;
3535
$this->authorization = $authorization;
3636
}
37-
37+
3838
/**
3939
* @param string $endpoint API URL
4040
* @return \Bpi\Sdk\Document same instance
@@ -57,7 +57,7 @@ public function loadEndpoint($endpoint)
5757
public function request($method, $uri, array $params = array())
5858
{
5959
$headers = array(
60-
'HTTP_Authorization' => $this->authorization->toHTTPHeader(),
60+
'HTTP_Auth' => $this->authorization->toHTTPHeader(),
6161
'HTTP_Content_Type' => 'application/vnd.bpi.api+xml',
6262
);
6363

@@ -67,7 +67,7 @@ public function request($method, $uri, array $params = array())
6767

6868
return $this;
6969
}
70-
70+
7171
/**
7272
* Dump latest raw response data
7373
*
@@ -77,6 +77,11 @@ public function dumpRawResponse()
7777
{
7878
return $this->http_client->getResponse();
7979
}
80+
public function dumpRawRequest()
81+
{
82+
return $this->http_client->getRequest();
83+
}
84+
8085

8186
/**
8287
* Access hypermedia link.
@@ -100,7 +105,7 @@ public function link($rel)
100105
throw new Exception\UndefinedHypermedia();
101106
}
102107
}
103-
108+
104109
/**
105110
* Click on link.
106111
*
@@ -110,7 +115,7 @@ public function followLink(Link $link)
110115
{
111116
$link->follow($this);
112117
}
113-
118+
114119
/**
115120
* Access hypermedia query.
116121
*
@@ -137,7 +142,7 @@ public function query($rel)
137142

138143
/**
139144
* Send query.
140-
*
145+
*
141146
* @param \Bpi\Sdk\Query $query
142147
* @param array $params
143148
*/
@@ -182,7 +187,7 @@ public function postTemplate(Template $template)
182187

183188
/**
184189
* Checks current item type
185-
*
190+
*
186191
* @param string $type
187192
* @return bool
188193
*/
@@ -235,12 +240,11 @@ public function firstItem($attr, $value) {
235240
* @return \Bpi\Sdk\Document same instance
236241
*/
237242
public function reduceItemsByAttr($attr, $value) {
238-
$this->crawler = $this->crawler
239-
->filter("item[$attr='{$value}']")
240-
;
243+
$this->crawler = $this->crawler->filter("item[$attr='{$value}']");
241244

242-
if (!$this->crawler->count())
245+
if (!$this->crawler->count()) {
243246
throw new \InvalidArgumentException();
247+
}
244248

245249
$this->crawler->rewind();
246250
return $this;
@@ -258,49 +262,49 @@ function rewind()
258262

259263
/**
260264
* Returns same instance but with internal pointer to current item in collection
261-
*
265+
*
262266
* @group Iterator
263267
* @return \Bpi\Sdk\Document will return same instance
264268
*/
265-
function current()
269+
function current()
266270
{
267271
return $this;
268272
}
269273

270274
/**
271275
* Key of current iteration position
272-
*
276+
*
273277
* @group Iterator
274278
*/
275-
function key()
279+
function key()
276280
{
277281
return $this->crawler->key();
278282
}
279283

280284
/**
281285
* Iterate to next item
282-
*
286+
*
283287
* @group Iterator
284288
*/
285-
function next()
289+
function next()
286290
{
287291
$this->crawler->next();
288292
}
289293

290294
/**
291295
* Checks if is ready for iteration
292-
*
296+
*
293297
* @group Iterator
294298
* @return boolean
295299
*/
296-
function valid()
300+
function valid()
297301
{
298302
return $this->crawler->valid();
299303
}
300-
304+
301305
/**
302306
* Length of items in document
303-
*
307+
*
304308
* @group Iterator
305309
*/
306310
public function count()

0 commit comments

Comments
 (0)