Completed
Push — master ( e142c8...96be6a )
by Dragos
03:23
created

ArticleService   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 10

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 0 Features 3
Metric Value
wmc 11
c 4
b 0
f 3
lcom 1
cbo 10
dl 0
loc 132
ccs 36
cts 36
cp 1
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A getCheckoutURL() 0 4 1
A getArticles() 0 9 1
A getArticle() 0 14 2
A getArticleCheckoutURL() 0 8 2
A getArticleNumberCheckoutURL() 0 9 2
A generateCheckoutURLForCustomer() 0 9 1
A getSubscriptionProductChangeURL() 0 4 1
A generateSubscriptionProductChangeURL() 0 9 1
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
use Speicher210\Fastbill\Api\Model\Subscription;
9
10
/**
11
 * Service for articles.
12
 */
13
class ArticleService extends AbstractService
14
{
15
    /**
16
     * Get the articles.
17
     *
18
     * @param string $articleNumber Article number to filter on.
19
     * @return Get\ApiResponse
20
     */
21 3
    public function getArticles($articleNumber = null)
22
    {
23 3
        $requestData = new Get\RequestData();
24 3
        $requestData->setArticleNumber($articleNumber);
25
26 3
        $request = new Get\Request($requestData);
27
28 3
        return $this->sendRequest($request, Get\ApiResponse::class);
29
    }
30
31
    /**
32
     * Get one article by article number.
33
     *
34
     * @param string $articleNumber Article number of the article to get.
35
     * @return Article|null
36
     */
37 12
    public function getArticle($articleNumber)
38
    {
39 12
        $requestData = new Get\RequestData();
40 12
        $requestData->setArticleNumber($articleNumber);
41
42 12
        $request = new Get\Request($requestData);
43 12
        $request->setLimit(1);
44
45
        /** @var Get\ApiResponse $apiResponse */
46 12
        $apiResponse = $this->sendRequest($request, Get\ApiResponse::class);
47 12
        $articles = $apiResponse->getResponse()->getArticles();
48
49 12
        return isset($articles[0]) ? $articles[0] : null;
50
    }
51
52
    /**
53
     * Get the article checkout URL.
54
     *
55
     * @param Article $article The article.
56
     * @param Customer|null $customer The customer.
57
     * @return string
58
     */
59 9
    public function getArticleCheckoutURL(Article $article, Customer $customer = null)
60
    {
61 9
        if ($customer === null) {
62 3
            return $article->getCheckoutUrl();
63
        }
64
65 6
        return $this->generateCheckoutURLForCustomer($customer->getCustomerId(), $article->getArticleNumber());
66
    }
67
68
    /**
69
     * Get the checkout URL for an article.
70
     *
71
     * @param string $articleNumber Article number of the article to get the checkout URL.
72
     * @param Customer $customer The customer for which the checkout URL should be created.
73
     * @return string
74
     */
75 9
    public function getArticleNumberCheckoutURL($articleNumber, Customer $customer = null)
76
    {
77 9
        $article = $this->getArticle($articleNumber);
78 9
        if ($article === null) {
79 3
            throw new \OutOfBoundsException('Article not found.');
80
        }
81
82 6
        return $this->getArticleCheckoutURL($article, $customer);
83
    }
84
85
    /**
86
     * Get the checkout URL for an article.
87
     *
88
     * @deprecated Use getArticleNumberCheckoutURL
89
     *
90
     * @param string $articleNumber Article number of the article to get the checkout URL.
91
     * @param Customer $customer The customer for which the checkout URL should be created.
92
     * @return string
93
     */
94
    public function getCheckoutURL($articleNumber, Customer $customer = null)
95
    {
96
        return $this->getArticleNumberCheckoutURL($articleNumber, $customer);
97
    }
98
99
    /**
100
     * Get the checkout URL of an article for a customer.
101
     *
102
     * @param string $customerId The customer ID.
103
     * @param string $articleNumber The article number.
104
     * @return string
105
     */
106 6
    protected function generateCheckoutURLForCustomer($customerId, $articleNumber)
107
    {
108 6
        return sprintf(
109 6
            'https://automatic.fastbill.com/checkout/0/%s/%s/%s',
110 6
            $this->transport->getCredentials()->getAccountHash(),
111 6
            $customerId,
112
            $articleNumber
113 6
        );
114
    }
115
116
    /**
117
     * Get the checkout URL to change a product for a subscription.
118
     *
119
     * @param Subscription $subscription The subscription.
120
     * @param Article $article The new article.
121
     * @return string
122
     */
123 3
    public function getSubscriptionProductChangeURL(Subscription $subscription, Article $article)
124
    {
125 3
        return $this->generateSubscriptionProductChangeURL($subscription->getSubscriptionId(), $article->getArticleNumber());
126
    }
127
128
    /**
129
     * Get the checkout URL to change a product for a subscription.
130
     *
131
     * @param string $subscriptionId The subscription ID.
132
     * @param string $articleNumber The new article number.
133
     * @return string
134
     */
135 3
    protected function generateSubscriptionProductChangeURL($subscriptionId, $articleNumber)
136
    {
137 3
        return sprintf(
138 3
            'https://automatic.fastbill.com/change/%s/%s/%s',
139 3
            $this->transport->getCredentials()->getAccountHash(),
140 3
            $subscriptionId,
141
            $articleNumber
142 3
        );
143
    }
144
}
145