Test Setup Failed
Push — master ( 584547...730ac2 )
by Alexey
14:04
created

CrawlerController::__construct()   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 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 2
1
<?php
2
3
namespace Skobkin\Bundle\PointToolsBundle\Controller\Api;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use JMS\Serializer\SerializerInterface;
7
use Skobkin\Bundle\PointToolsBundle\DTO\Api\PostsPage;
8
use Skobkin\Bundle\PointToolsBundle\Service\Factory\Blogs\PostFactory;
9
use Symfony\Component\HttpFoundation\{Request, Response};
10
11
class CrawlerController extends AbstractApiController
12
{
13
    /** @var string */
14
    private $crawlerToken;
15
16
    public function __construct(string $crawlerToken)
17
    {
18
        $this->crawlerToken = $crawlerToken;
19
    }
20
21
    public function receiveAllPageAction(Request $request, SerializerInterface $serializer, PostFactory $postFactory, EntityManagerInterface $em): Response
22
    {
23
        $remoteToken = $request->request->get('token');
24
25
        if (!$this->crawlerToken || ($this->crawlerToken !== $remoteToken)) {
26
            return $this->createErrorResponse('Token error. Please check it in crawler and API parameters.', Response::HTTP_FORBIDDEN);
27
        }
28
29
        $json = $request->request->get('json');
30
31
        $page = $serializer->deserialize($json, PostsPage::class, 'json');
32
        
33
        $continue = $postFactory->createFromPageDTO($page);
34
35
        $em->flush();
36
37
        return $this->createSuccessResponse([
38
            'continue' => $continue,
39
        ]);
40
    }
41
}
42