These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /* |
||
3 | * WellCommerce Open-Source E-Commerce Platform |
||
4 | * |
||
5 | * This file is part of the WellCommerce package. |
||
6 | * |
||
7 | * (c) Adam Piotrowski <[email protected]> |
||
8 | * |
||
9 | * For the full copyright and license information, |
||
10 | * please view the LICENSE file that was distributed with this source code. |
||
11 | */ |
||
12 | |||
13 | namespace WellCommerce\Bundle\ReviewBundle\Controller\Box; |
||
14 | |||
15 | use Symfony\Component\Config\Definition\Exception\Exception; |
||
16 | use Symfony\Component\HttpFoundation\Cookie; |
||
17 | use Symfony\Component\HttpFoundation\JsonResponse; |
||
18 | use Symfony\Component\HttpFoundation\Response; |
||
19 | use WellCommerce\Bundle\CoreBundle\Controller\Box\AbstractBoxController; |
||
20 | use WellCommerce\Bundle\ReviewBundle\Entity\Review; |
||
21 | use WellCommerce\Bundle\ReviewBundle\Repository\ReviewRepositoryInterface; |
||
22 | use WellCommerce\Component\Layout\Collection\LayoutBoxSettingsCollection; |
||
23 | |||
24 | |||
25 | /** |
||
26 | * Class ReviewBoxController |
||
27 | * |
||
28 | * @author Adam Piotrowski <[email protected]> |
||
29 | */ |
||
30 | class ReviewBoxController extends AbstractBoxController |
||
31 | { |
||
32 | const RECOMMENDATION_COOKIE_EXPIRE = 86400 * 30 * 12 * 4; |
||
33 | |||
34 | public function indexAction(LayoutBoxSettingsCollection $boxSettings): Response |
||
35 | { |
||
36 | $product = $this->getProductStorage()->getCurrentProduct(); |
||
37 | |||
38 | /** @var Review $resource */ |
||
39 | $resource = $this->getManager()->initResource(); |
||
40 | $resource->setProduct($product); |
||
41 | |||
42 | $currentRoute = $product->translate()->getRoute()->getId(); |
||
43 | $form = $this->formBuilder->createForm($resource); |
||
44 | |||
45 | if ($form->handleRequest()->isSubmitted()) { |
||
46 | if ($form->isValid()) { |
||
47 | if (false === $this->get('security.authorization_checker')->isGranted('ROLE_CLIENT') |
||
48 | && false === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN') |
||
49 | ) { |
||
50 | $resource->setEnabled(0); |
||
51 | } |
||
52 | $this->getManager()->createResource($resource); |
||
53 | |||
54 | $this->getFlashHelper()->addSuccess('review.flash.success'); |
||
55 | |||
56 | return $this->getRouterHelper()->redirectTo('dynamic_' . $currentRoute); |
||
57 | } |
||
58 | |||
59 | $this->getFlashHelper()->addError('review.flash.error'); |
||
60 | } |
||
61 | |||
62 | return $this->displayTemplate('index', [ |
||
63 | 'form' => $form, |
||
64 | 'product' => $product, |
||
65 | 'reviews' => $this->getRepository()->getProductReviews($product), |
||
66 | 'boxSettings' => $boxSettings, |
||
67 | ]); |
||
68 | } |
||
69 | |||
70 | public function reportAction(int $id) |
||
71 | { |
||
72 | $review = $this->getManager()->getRepository()->findOneBy([ |
||
73 | 'id' => $id, |
||
74 | 'enabled' => 1, |
||
75 | ]); |
||
76 | |||
77 | if ($review instanceof Review) { |
||
78 | $currentRoute = $review->getProduct()->translate()->getRoute()->getId(); |
||
79 | $mailerConfiguration = $this->getShopStorage()->getCurrentShop()->getMailerConfiguration(); |
||
80 | |||
81 | $this->getMailerHelper()->sendEmail([ |
||
82 | 'recipient' => $mailerConfiguration->getFrom(), |
||
83 | 'subject' => $this->trans('review.email.heading.report'), |
||
84 | 'template' => 'WellCommerceAppBundle:Email:report_review.html.twig', |
||
85 | 'parameters' => [ |
||
86 | 'review' => $review, |
||
87 | ], |
||
88 | 'configuration' => $mailerConfiguration, |
||
89 | ]); |
||
90 | |||
91 | $this->getFlashHelper()->addSuccess('report.flash.success'); |
||
92 | |||
93 | return $this->redirectToRoute('dynamic_' . $currentRoute); |
||
94 | } |
||
95 | |||
96 | return $this->redirectToRoute('front.home_page.index'); |
||
97 | } |
||
98 | |||
99 | public function recommendationAction(int $id, bool $like = true): JsonResponse |
||
100 | { |
||
101 | try { |
||
102 | $cookie = $this->getRequestHelper()->getCurrentRequest()->cookies->get('likedReviews'); |
||
103 | $reviewLiked = unserialize($cookie); |
||
104 | |||
105 | if (!is_array($reviewLiked)) { |
||
106 | $reviewLiked = []; |
||
107 | } |
||
108 | |||
109 | if (in_array($id, $reviewLiked)) { |
||
110 | throw new Exception($this->trans('review.label.recommendation_exists')); |
||
111 | } |
||
112 | |||
113 | $review = $this->getManager()->getRepository()->findOneBy([ |
||
114 | 'id' => $id, |
||
115 | 'enabled' => 1, |
||
116 | ]); |
||
117 | |||
118 | if (null === $review) { |
||
119 | throw new Exception($this->trans('review.label.review_not_exists')); |
||
120 | } |
||
121 | |||
122 | $reviewRecommendationFactory = $this->get('review_recommendation.manager'); |
||
123 | $reviewRecommendation = $reviewRecommendationFactory->initResource(); |
||
124 | |||
125 | if ($like == true) { |
||
0 ignored issues
–
show
|
|||
126 | $reviewRecommendation->setLiked(true); |
||
127 | } else { |
||
128 | $reviewRecommendation->setUnliked(true); |
||
129 | } |
||
130 | $reviewRecommendation->setReview($review); |
||
131 | $this->getManager()->updateResource($reviewRecommendation); |
||
132 | $this->getManager()->updateResource($review); |
||
133 | $review->addRecommendation($reviewRecommendation); |
||
134 | |||
135 | $reviewLikes = $reviewRecommendationFactory->getRepository()->findBy([ |
||
136 | 'review' => $review, |
||
137 | 'liked' => true, |
||
138 | ]); |
||
139 | |||
140 | $reviewUnlikes = $reviewRecommendationFactory->getRepository()->findBy([ |
||
141 | 'review' => $review, |
||
142 | 'unliked' => true, |
||
143 | ]); |
||
144 | |||
145 | $result = [ |
||
146 | 'success' => true, |
||
147 | 'likes' => count($reviewLikes), |
||
148 | 'unlikes' => count($reviewUnlikes), |
||
149 | ]; |
||
150 | } catch (Exception $e) { |
||
151 | $result = [ |
||
152 | 'error' => true, |
||
153 | 'message' => $e->getMessage(), |
||
154 | ]; |
||
155 | } |
||
156 | |||
157 | array_push($reviewLiked, $id); |
||
158 | |||
159 | $response = $this->jsonResponse($result); |
||
160 | $response->headers->setCookie(new Cookie('likedReviews', serialize($reviewLiked), time() + static::RECOMMENDATION_COOKIE_EXPIRE)); |
||
161 | |||
162 | return $response; |
||
163 | } |
||
164 | |||
165 | private function getRepository(): ReviewRepositoryInterface |
||
166 | { |
||
167 | return $this->getManager()->getRepository(); |
||
168 | } |
||
169 | } |
||
170 |
When comparing two booleans, it is generally considered safer to use the strict comparison operator.