Completed
Push — master ( 5ab7f0...7535f2 )
by Dragos
08:13
created

ArticleService::getArticleNumberCheckoutURL()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 9.6666
cc 2
eloc 5
nc 2
nop 2
crap 2
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
     * @return Get\ApiResponse
19
     */
20 3
    public function getArticles($articleNumber = null)
21
    {
22 3
        $requestData = new Get\RequestData();
23 3
        $requestData->setArticleNumber($articleNumber);
24
25 3
        $request = new Get\Request($requestData);
26
27 3
        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 12
    public function getArticle($articleNumber)
37
    {
38 12
        $requestData = new Get\RequestData();
39 12
        $requestData->setArticleNumber($articleNumber);
40
41 12
        $request = new Get\Request($requestData);
42 12
        $request->setLimit(1);
43
44
        /** @var Get\ApiResponse $apiResponse */
45 12
        $apiResponse = $this->sendRequest($request, Get\ApiResponse::class);
46 12
        $articles = $apiResponse->getResponse()->getArticles();
47
48 12
        return isset($articles[0]) ? $articles[0] : null;
49
    }
50
51 9
    public function getArticleCheckoutURL(Article $article, Customer $customer = null)
52
    {
53 9
        if ($customer === null) {
54 3
            return $article->getCheckoutUrl();
55
        }
56
57 6
        return $this->generateCheckoutURLForCustomer($customer->getHash(), $article->getArticleNumber());
58
    }
59
60
    /**
61
     * Get the checkout URL for an article.
62
     *
63
     * @param string $articleNumber Article number of the article to get the checkout URL.
64
     * @param Customer $customer The customer for witch the checkout URL should be created.
65
     * @return string
66
     */
67 9
    public function getArticleNumberCheckoutURL($articleNumber, Customer $customer = null)
68
    {
69 9
        $article = $this->getArticle($articleNumber);
70 9
        if ($article === null) {
71 3
            throw new \OutOfBoundsException('Article not found.');
72
        }
73
74 6
        return $this->getArticleCheckoutURL($article, $customer);
75
    }
76
77
    /**
78
     * Get the checkout URL for an article.
79
     *
80
     * @deprecated Use getArticleNumberCheckoutURL
81
     *
82
     * @param string $articleNumber Article number of the article to get the checkout URL.
83
     * @param Customer $customer The customer for witch the checkout URL should be created.
84
     * @return string
85
     */
86
    public function getCheckoutURL($articleNumber, Customer $customer = null)
87
    {
88
        return $this->getArticleNumberCheckoutURL($articleNumber, $customer);
89
    }
90
91
    /**
92
     * Get the checkout URL of an article for a customer.
93
     *
94
     * @param string $customerHash The customer hash.
95
     * @param string $articleNumber The article number.
96
     * @return string
97
     */
98 6
    protected function generateCheckoutURLForCustomer($customerHash, $articleNumber)
99
    {
100 6
        return sprintf(
101 6
            'https://automatic.fastbill.com/checkout/0/%s/%s/%s',
102 6
            $this->transport->getCredentials()->getAccountHash(),
103 6
            $customerHash,
104
            $articleNumber
105 6
        );
106
    }
107
}
108