Completed
Push — master ( f95e19...31ed8c )
by Dragos
13:13
created

ArticleService::getCheckoutURL()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 18
ccs 0
cts 0
cp 0
rs 9.4285
cc 3
eloc 11
nc 3
nop 2
crap 12
1
<?php
2
3
namespace Speicher210\Fastbill\Api\Service\Article;
4
5
use Speicher210\Fastbill\Api\AbstractService;
6
use Speicher210\Fastbill\Api\Model\Article;
7
use Speicher210\Fastbill\Api\Model\Customer;
8
9
/**
10
 * Service for articles.
11
 */
12
class ArticleService extends AbstractService
13
{
14
    /**
15
     * Get the articles.
16
     *
17
     * @param string $articleNumber Article number to filter on.
18 3
     * @return Get\ApiResponse
19
     */
20 3
    public function getArticles($articleNumber = null)
21 3
    {
22
        $requestData = new Get\RequestData();
23 3
        $requestData->setArticleNumber($articleNumber);
24
25 3
        $request = new Get\Request($requestData);
26
27
        return $this->sendRequest($request, Get\ApiResponse::class);
28
    }
29
30
    /**
31
     * Get one article by article number.
32
     *
33
     * @param string $articleNumber Article number of the article to get.
34
     * @return Article|null
35
     */
36
    public function getArticle($articleNumber)
37
    {
38
        $requestData = new Get\RequestData();
39
        $requestData->setLimit(1);
40
        $requestData->setArticleNumber($articleNumber);
41
42
        $request = new Get\Request($requestData);
43
44
        /** @var Get\ApiResponse $apiResponse */
45
        $apiResponse = $this->sendRequest($request, Get\ApiResponse::class);
46
        $articles = $apiResponse->getResponse()->getArticles();
47
48
        return isset($articles[0]) ? $articles[0] : null;
49
    }
50
51
    /**
52
     * Get the checkout URL for an article.
53
     *
54
     * @param string $articleNumber Article number of the article to get the checkout URL.
55
     * @param Customer $customer The customer for witch the checkout URL should be created.
56
     * @return string
57
     */
58
    public function getCheckoutURL($articleNumber, Customer $customer = null)
59
    {
60
        $article = $this->getArticle($articleNumber);
61
        if ($article === null) {
62
            throw new \OutOfBoundsException('Article not found.');
63
        }
64
65
        if ($customer === null) {
66
            return $article->getCheckoutUrl();
67
        }
68
69
        return sprintf(
70
            'https://automatic.fastbill.com/checkout/0/%s/%s/%s',
71
            $this->transport->getCredentials()->getAccountHash(),
72
            $customer->getHash(),
73
            $articleNumber
74
        );
75
    }
76
}
77