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

CrawlerController::receiveAllPageAction()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 0
cts 7
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 10
nc 2
nop 4
crap 12
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