Completed
Branch 1707_add_explain_command (4124c6)
by Alessandro
02:34
created

ProfilerController::walkAndConvertToUTCDatetime()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 0
cts 15
cp 0
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 4
nop 1
crap 30
1
<?php declare(strict_types=1);
2
3
namespace Facile\MongoDbBundle\Controller;
4
5
use Facile\MongoDbBundle\DataCollector\MongoQuerySerializer;
6
use Facile\MongoDbBundle\Services\ExplainQueryService;
7
use MongoDB\BSON\UTCDateTime;
8
use Symfony\Component\DependencyInjection\Container;
9
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Symfony\Component\HttpFoundation\JsonResponse;
12
13
class ProfilerController implements ContainerAwareInterface
14
{
15
    /** @var Container */
16
    private $container;
17
18
    /**
19
     * Sets the container.
20
     *
21
     * @param ContainerInterface|null $container A ContainerInterface instance or null
22
     */
23
    public function setContainer(ContainerInterface $container = null)
24
    {
25
        $this->container = $container;
0 ignored issues
show
Documentation Bug introduced by
It seems like $container can also be of type object<Symfony\Component...ion\ContainerInterface>. However, the property $container is declared as type object<Symfony\Component...ncyInjection\Container>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
26
    }
27
28
    public function explainAction($token, $queryNumber)
29
    {
30
        /** @var $profiler \Symfony\Component\HttpKernel\Profiler\Profiler */
31
        $profiler = $this->container->get('profiler');
32
        $profiler->disable();
33
34
        $profile = $profiler->loadProfile($token);
35
        $queries = $profile->getCollector('mongodb')->getQueries();
36
37
        $query = $queries[$queryNumber];
38
39
        $query->setFilters($this->walkAndConvertToUTCDatetime($query->getFilters()));
40
41
        $service = new ExplainQueryService($this->container->get('mongo.client_registry'));
42
43
        try{
44
            $result = $service->execute($query);
45
        } catch (\InvalidArgumentException $e) {
46
            return new JsonResponse([
47
                "err" => $e->getMessage()
48
            ]);
49
        }
50
51
        return new JsonResponse(
52
            json_encode(
53
                MongoQuerySerializer::prepareItemData($result->toArray()),
54
                JSON_PRETTY_PRINT
55
            ),
56
            200,
57
            [],
58
            true
59
        );
60
    }
61
62
    /**
63
     * @param array $data
64
     *
65
     * @return array
66
     */
67
    private function walkAndConvertToUTCDatetime($data)
68
    {
69
        if (!is_array($data)) {
70
            return $data;
71
        }
72
73
        foreach ($data as $key => $item) {
74
75
            if (is_string($item) && preg_match('/^ISODate/', $item)) {
76
                $time = str_replace(['ISODate("','")'], '', $item);
77
                $dateTime = new \DateTime($time);
78
                $item = new UTCDatetime($dateTime->getTimestamp() * 1000);
79
            }
80
81
            $data[$key] = $this->walkAndConvertToUTCDatetime($item);
82
        }
83
84
        return $data;
85
    }
86
}