Completed
Push — master ( 29bc6b...e2df67 )
by Dragos
13:33
created

ArticleService::getSubscriptionProductChangeURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
cc 1
eloc 2
nc 1
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
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 3
     */
21
    public function getArticles($articleNumber = null)
22 3
    {
23 3
        $requestData = new Get\RequestData();
24
        $requestData->setArticleNumber($articleNumber);
25 3
26
        $request = new Get\Request($requestData);
27 3
28
        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 12
     */
37
    public function getArticle($articleNumber)
38 12
    {
39 12
        $requestData = new Get\RequestData();
40
        $requestData->setArticleNumber($articleNumber);
41 12
42 12
        $request = new Get\Request($requestData);
43
        $request->setLimit(1);
44
45 12
        /** @var Get\ApiResponse $apiResponse */
46 12
        $apiResponse = $this->sendRequest($request, Get\ApiResponse::class);
47
        $articles = $apiResponse->getResponse()->getArticles();
48 12
49
        return isset($articles[0]) ? $articles[0] : null;
50
    }
51 9
52
    /**
53 9
     * Get the article checkout URL.
54 3
     *
55
     * @param Article $article The article.
56
     * @param Customer|null $customer The customer.
57 6
     * @return string
58
     */
59
    public function getArticleCheckoutURL(Article $article, Customer $customer = null)
60
    {
61
        if ($customer === null) {
62
            return $article->getCheckoutUrl();
63
        }
64
65
        return $this->generateCheckoutURLForCustomer($customer->getCustomerId(), $article->getArticleNumber());
66
    }
67 9
68
    /**
69 9
     * Get the checkout URL for an article.
70 9
     *
71 3
     * @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 6
     */
75
    public function getArticleNumberCheckoutURL($articleNumber, Customer $customer = null)
76
    {
77
        $article = $this->getArticle($articleNumber);
78
        if ($article === null) {
79
            throw new \OutOfBoundsException('Article not found.');
80
        }
81
82
        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 6
99
    /**
100 6
     * Get the checkout URL of an article for a customer.
101 6
     *
102 6
     * @param string $customerId The customer ID.
103 6
     * @param string $articleNumber The article number.
104
     * @return string
105 6
     */
106
    protected function generateCheckoutURLForCustomer($customerId, $articleNumber)
107
    {
108
        return sprintf(
109
            'https://automatic.fastbill.com/checkout/0/%s/%s/%s',
110
            $this->transport->getCredentials()->getAccountHash(),
111
            $customerId,
112
            $articleNumber
113
        );
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
    public function getSubscriptionProductChangeURL(Subscription $subscription, Article $article)
124
    {
125
        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
    protected function generateSubscriptionProductChangeURL($subscriptionId, $articleNumber)
136
    {
137
        return sprintf(
138
            'https://automatic.fastbill.com/checkout/0/%s/%s/%s',
139
            $this->transport->getCredentials()->getAccountHash(),
140
            $subscriptionId,
141
            $articleNumber
142
        );
143
    }
144
}
145