1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace PTS\SyliusReferralPlugin\Service; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use PTS\SyliusReferralPlugin\Entity\Customer; |
9
|
|
|
use Sylius\Component\Core\Model\ShopUser; |
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
|
12
|
|
|
class CustomerManager |
13
|
|
|
{ |
14
|
|
|
private $entityManager; |
15
|
|
|
|
16
|
|
|
public function __construct(EntityManager $entityManager) |
17
|
|
|
{ |
18
|
|
|
$this->entityManager = $entityManager; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function getCustomerReferrals(Request $request, $route, $id) |
22
|
|
|
{ |
23
|
|
|
$customer = $this->entityManager->getRepository(Customer::class)->findOneBy(['id' => $id]); |
24
|
|
|
if ($customer) { |
25
|
|
|
$page = $request->get('page'); |
26
|
|
|
$data = $this->getPaginatedCustomers($customer, $page, $route, $id); |
27
|
|
|
return $data; |
28
|
|
|
} |
29
|
|
|
} |
30
|
|
|
public function getEnroller($id) { |
31
|
|
|
$customerRepo = $this->entityManager->getRepository(Customer::class); |
32
|
|
|
/** @var Customer $customer */ |
33
|
|
|
$customer = $customerRepo->findOneBy(['id' => $id]); |
34
|
|
|
if ($enroller = $customer->getEnroller()) { |
35
|
|
|
return $enroller; |
36
|
|
|
} |
37
|
|
|
return null; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function getPaginatedCustomers($enroller, $page, $routeName, $id = null) |
41
|
|
|
{ |
42
|
|
|
if (!$page || $page < 0) { |
43
|
|
|
$page = 1; |
44
|
|
|
} |
45
|
|
|
$limit = 7; |
46
|
|
|
$customers = $this->entityManager |
47
|
|
|
->getRepository(Customer::class) |
48
|
|
|
->getEnrolled($enroller, $limit, floor($page)); |
|
|
|
|
49
|
|
|
$maxPages = ceil($customers->count() / $limit); |
50
|
|
|
$thisPage = (int)$page; |
51
|
|
|
if ($maxPages < $thisPage) { |
52
|
|
|
$thisPage = $maxPages; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return [ |
56
|
|
|
'customers' => iterator_to_array($customers->getIterator()), |
57
|
|
|
'pagination' => [ |
58
|
|
|
'maxPages' => $maxPages, |
59
|
|
|
'thisPage' => $thisPage, |
60
|
|
|
'route' => $routeName, |
61
|
|
|
'id' => $id |
62
|
|
|
] |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
public function changeCustomerEnroller($customerId, $enrollerId) |
66
|
|
|
{ |
67
|
|
|
$customerRepo = $this->entityManager |
68
|
|
|
->getRepository(Customer::class); |
69
|
|
|
/** @var Customer $customer */ |
70
|
|
|
$customer = $customerRepo |
71
|
|
|
->findOneBy(['id' => $customerId]); |
72
|
|
|
$enroller = $customerRepo |
73
|
|
|
->findOneBy(['id' => $enrollerId]); |
74
|
|
|
$customer->setEnroller($enroller); |
75
|
|
|
$this->entityManager->flush(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
} |