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

ArticleService   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 6
c 2
b 0
f 1
lcom 1
cbo 9
dl 0
loc 65
ccs 5
cts 5
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getArticles() 0 9 1
A getArticle() 0 14 2
A getCheckoutURL() 0 18 3
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