|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PTS\SyliusReferralPlugin\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Sylius\Bundle\CoreBundle\Doctrine\ORM\CustomerRepository; |
|
6
|
|
|
use Sylius\Bundle\ProductBundle\Doctrine\ORM\ProductRepository; |
|
7
|
|
|
use Sylius\Bundle\ResourceBundle\Storage\SessionStorage; |
|
8
|
|
|
use Sylius\Component\Core\Model\Product; |
|
9
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
10
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
|
11
|
|
|
|
|
12
|
|
|
class ReferralManager |
|
13
|
|
|
{ |
|
14
|
|
|
static private $expirationDays = 7 * (24*60*60); // 7 days (h*m*s) |
|
15
|
|
|
|
|
16
|
|
|
/** @var SessionStorage */ |
|
17
|
|
|
private $session; |
|
18
|
|
|
|
|
19
|
|
|
/** @var CustomerRepository */ |
|
20
|
|
|
private $customerRepo; |
|
21
|
|
|
|
|
22
|
|
|
/** @var TokenStorage */ |
|
23
|
|
|
private $tokenStorage; |
|
24
|
|
|
|
|
25
|
|
|
/** @var UrlGeneratorInterface */ |
|
26
|
|
|
private $router; |
|
27
|
|
|
|
|
28
|
|
|
/** @var ProductRepository */ |
|
29
|
|
|
private $productRepo; |
|
30
|
|
|
|
|
31
|
|
|
private $channelPaths; |
|
32
|
|
|
|
|
33
|
|
|
/** @var ChannelUrlManager */ |
|
34
|
|
|
private $channelUrlManager; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* ReferralManager constructor. |
|
38
|
|
|
* @param SessionStorage $session |
|
39
|
|
|
* @param CustomerRepository $customerRepo |
|
40
|
|
|
* @param TokenStorage $tokenStorage |
|
41
|
|
|
* @param UrlGeneratorInterface $router |
|
42
|
|
|
* @param ProductRepository $productRepo |
|
43
|
|
|
* @param $channelPaths |
|
44
|
|
|
* @param $channelUrlManager |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __construct(SessionStorage $session, |
|
47
|
|
|
CustomerRepository $customerRepo, |
|
48
|
|
|
TokenStorage $tokenStorage, |
|
49
|
|
|
UrlGeneratorInterface $router, |
|
50
|
|
|
ProductRepository $productRepo, |
|
51
|
|
|
$channelPaths, |
|
52
|
|
|
$channelUrlManager |
|
53
|
|
|
) { |
|
54
|
|
|
$this->session = $session; |
|
55
|
|
|
$this->customerRepo = $customerRepo; |
|
56
|
|
|
$this->tokenStorage = $tokenStorage; |
|
57
|
|
|
$this->router = $router; |
|
58
|
|
|
$this->productRepo = $productRepo; |
|
59
|
|
|
$this->channelPaths = $channelPaths; |
|
60
|
|
|
$this->channelUrlManager = $channelUrlManager; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
|
|
64
|
|
|
public function getReferrerFromSession() |
|
65
|
|
|
{ |
|
66
|
|
|
if($this->session->has('referrer')) { |
|
67
|
|
|
$saved = json_decode($this->session->get('referrer'), true); |
|
68
|
|
|
return $this->customerRepo->findOneBy(['id' => $saved['id']]); |
|
69
|
|
|
} |
|
70
|
|
|
return null; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
public function setReferral($id) |
|
74
|
|
|
{ |
|
75
|
|
|
$data = [ |
|
76
|
|
|
'timestamp' => time(), |
|
77
|
|
|
'id' => $id |
|
78
|
|
|
]; |
|
79
|
|
|
if ($this->session->has('referrer')) { |
|
80
|
|
|
$saved = json_decode($this->session->get('referrer'), true); |
|
81
|
|
|
if ($saved['id'] == $id) { |
|
82
|
|
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
$this->session->set('referrer', json_encode($data)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function checkReferralValidity() |
|
89
|
|
|
{ |
|
90
|
|
|
if ($this->session->has('referrer')) { |
|
91
|
|
|
$referrer = json_decode($this->session->get('referrer'), true); |
|
92
|
|
|
$setTime = $referrer['timestamp']; |
|
93
|
|
|
if (time() - $setTime > $this::$expirationDays) { |
|
94
|
|
|
$this->removeReferral(); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function removeReferral() |
|
100
|
|
|
{ |
|
101
|
|
|
if ($this->session->has('referrer')) { |
|
102
|
|
|
$this->session->remove('referrer'); |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public function generateProductLink($product, $customer, $locale = null, $basePath = '') |
|
107
|
|
|
{ |
|
108
|
|
|
$distributorId = $customer->getid(); |
|
109
|
|
|
$routeParams = [ |
|
110
|
|
|
'refId' => $distributorId, |
|
111
|
|
|
'slug' => $product->getSlug() |
|
112
|
|
|
]; |
|
113
|
|
|
if ($locale) { |
|
114
|
|
|
$routeParams['_locale'] = $locale; |
|
115
|
|
|
} |
|
116
|
|
|
$url = $this->router->generate( |
|
117
|
|
|
'sylius_shop_product_show', |
|
118
|
|
|
$routeParams, |
|
119
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
|
120
|
|
|
); |
|
121
|
|
|
$requestUri = $this->router->generate( |
|
122
|
|
|
'sylius_shop_product_show', |
|
123
|
|
|
$routeParams, |
|
124
|
|
|
UrlGeneratorInterface::ABSOLUTE_PATH |
|
125
|
|
|
); |
|
126
|
|
|
return $this->trimChannelName($url, $requestUri, $basePath); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function referralProductLinkGenerator($id) |
|
130
|
|
|
{ |
|
131
|
|
|
$customer = $this->tokenStorage->getToken()->getUser()->getCustomer(); |
|
132
|
|
|
/** @var Product $product */ |
|
133
|
|
|
$product = $this->productRepo->find($id); |
|
134
|
|
|
return $this->generateProductLink($product, $customer); |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function generateRootLink($customer, $locale = null, $basePath = '') |
|
138
|
|
|
{ |
|
139
|
|
|
|
|
140
|
|
|
$distributorId = $customer->getid(); |
|
141
|
|
|
$routeParams = [ |
|
142
|
|
|
'refId' => $distributorId |
|
143
|
|
|
]; |
|
144
|
|
|
if ($locale) { |
|
145
|
|
|
$routeParams['_locale'] = $locale; |
|
146
|
|
|
} |
|
147
|
|
|
$url = $this->router->generate( |
|
148
|
|
|
'sylius_shop_homepage', |
|
149
|
|
|
$routeParams, |
|
150
|
|
|
UrlGeneratorInterface::ABSOLUTE_URL |
|
151
|
|
|
); |
|
152
|
|
|
$requestUri = $this->router->generate( |
|
153
|
|
|
'sylius_shop_homepage', |
|
154
|
|
|
$routeParams, |
|
155
|
|
|
UrlGeneratorInterface::ABSOLUTE_PATH |
|
156
|
|
|
); |
|
157
|
|
|
return $this->trimChannelName($url, $requestUri, $basePath); |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
public function referralRootLinkGenerator() |
|
161
|
|
|
{ |
|
162
|
|
|
$customer = $this->tokenStorage->getToken()->getUser()->getCustomer(); |
|
163
|
|
|
return $this->generateRootLink($customer); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
private function trimChannelName($url, $requestUri, $basePath) |
|
167
|
|
|
{ |
|
168
|
|
|
$urlParts = []; |
|
169
|
|
|
$splitUrl = explode('://', $url); |
|
170
|
|
|
$scheme = $splitUrl[0]; |
|
171
|
|
|
$urlToBeSplit = $splitUrl[1]; |
|
172
|
|
|
$urlParts['scheme'] = $scheme; |
|
173
|
|
|
$urlParts['host'] = substr($urlToBeSplit,0, strpos($urlToBeSplit,'/')); |
|
174
|
|
|
if ($basePath && $basePath !== '') { |
|
175
|
|
|
$urlParts['basePath'] = $basePath; |
|
176
|
|
|
} |
|
177
|
|
|
$urlParts['requestUri'] = $requestUri; |
|
178
|
|
|
return $this->channelUrlManager->formUrl($urlParts); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|