1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Odiseo\SyliusReferralsPlugin\Controller\Shop; |
6
|
|
|
|
7
|
|
|
use Odiseo\SyliusReferralsPlugin\Entity\AffiliateInterface; |
8
|
|
|
use Odiseo\SyliusReferralsPlugin\Repository\AffiliateReferralViewRepositoryInterface; |
9
|
|
|
use Odiseo\SyliusReferralsPlugin\Repository\OrderRepositoryInterface; |
10
|
|
|
use Sylius\Component\Core\Model\CustomerInterface; |
11
|
|
|
use Sylius\Component\Customer\Context\CustomerContextInterface; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Symfony\Component\HttpKernel\Exception\HttpException; |
15
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
16
|
|
|
use Twig\Environment; |
17
|
|
|
|
18
|
|
|
final class StatisticsAffiliateReferralAction |
19
|
|
|
{ |
20
|
|
|
private CustomerContextInterface $customerContext; |
21
|
|
|
private OrderRepositoryInterface $orderRepository; |
22
|
|
|
private AffiliateReferralViewRepositoryInterface $affiliateReferralViewRepository; |
23
|
|
|
private Environment $twig; |
24
|
|
|
|
25
|
|
|
public function __construct( |
26
|
|
|
CustomerContextInterface $customerContext, |
27
|
|
|
OrderRepositoryInterface $orderRepository, |
28
|
|
|
AffiliateReferralViewRepositoryInterface $affiliateReferralViewRepository, |
29
|
|
|
Environment $twig |
30
|
|
|
) { |
31
|
|
|
$this->customerContext = $customerContext; |
32
|
|
|
$this->orderRepository = $orderRepository; |
33
|
|
|
$this->affiliateReferralViewRepository = $affiliateReferralViewRepository; |
34
|
|
|
$this->twig = $twig; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function __invoke(Request $request): Response |
38
|
|
|
{ |
39
|
|
|
/** @var CustomerInterface|null $customer */ |
40
|
|
|
$customer = $this->customerContext->getCustomer(); |
41
|
|
|
if (null === $customer) { |
42
|
|
|
throw new HttpException(Response::HTTP_UNAUTHORIZED); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (!$customer instanceof AffiliateInterface) { |
46
|
|
|
throw new NotFoundHttpException(); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$sales = $this->orderRepository->countSalesByAffiliate($customer); |
50
|
|
|
$visits = $this->affiliateReferralViewRepository->countViewsByAffiliate($customer); |
51
|
|
|
$average = $this->getAverage($sales, $visits); |
52
|
|
|
|
53
|
|
|
$data = [ |
54
|
|
|
'sales' => $sales, |
55
|
|
|
'visits' => $visits, |
56
|
|
|
'average' => $average |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
$content = $this->twig->render( |
60
|
|
|
'@OdiseoSyliusReferralsPlugin/Shop/Account/AffiliateReferral/Index/Statistics/_template.html.twig', |
61
|
|
|
$data |
62
|
|
|
); |
63
|
|
|
|
64
|
|
|
return new Response($content); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
private function getAverage(int $sales, int $visits): int |
68
|
|
|
{ |
69
|
|
|
if ($sales == 0 || $visits == 0) { |
70
|
|
|
return 0; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
return (int) (($sales / $visits) * 100); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|