Completed
Branch 1707_add_explain_command (cbe7fb)
by Alessandro
02:54
created

ProfilerController::walkAndConvertToUTCDatetime()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 5.675

Importance

Changes 0
Metric Value
dl 0
loc 19
ccs 7
cts 10
cp 0.7
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 4
nop 1
crap 5.675
1
<?php declare(strict_types=1);
2
3
namespace Facile\MongoDbBundle\Controller;
4
5
use Facile\MongoDbBundle\DataCollector\MongoQuerySerializer;
6
use MongoDB\BSON\UTCDateTime;
7
use Symfony\Component\DependencyInjection\Container;
8
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\HttpFoundation\JsonResponse;
11
12
class ProfilerController implements ContainerAwareInterface
13
{
14
    /** @var Container */
15
    private $container;
16
17
    /**
18
     * Sets the container.
19
     *
20
     * @param ContainerInterface|null $container A ContainerInterface instance or null
21
     */
22 2
    public function setContainer(ContainerInterface $container = null)
23
    {
24 2
        $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...
25 2
    }
26
27 2
    public function explainAction($token, $queryNumber)
28
    {
29
        /** @var $profiler \Symfony\Component\HttpKernel\Profiler\Profiler */
30 2
        $profiler = $this->container->get('profiler');
31 2
        $profiler->disable();
32
33 2
        $profile = $profiler->loadProfile($token);
34 2
        $queries = $profile->getCollector('mongodb')->getQueries();
35
36 2
        $query = $queries[$queryNumber];
37
38 2
        $query->setFilters($this->walkAndConvertToUTCDatetime($query->getFilters()));
39
40 2
        $service = $this->container->get('mongo.explain_query_service');
41
42
        try{
43 2
            $result = $service->execute($query);
44 1
        } catch (\InvalidArgumentException $e) {
45 1
            return new JsonResponse([
46 1
                "err" => $e->getMessage()
47
            ]);
48
        }
49
50 1
        return new JsonResponse(MongoQuerySerializer::prepareItemData($result->toArray()));
51
    }
52
53
    /**
54
     * @param array $data
55
     *
56
     * @return array
57
     */
58 2
    private function walkAndConvertToUTCDatetime($data)
59
    {
60 2
        if (!is_array($data)) {
61 1
            return $data;
62
        }
63
64 2
        foreach ($data as $key => $item) {
65
66 1
            if (is_string($item) && preg_match('/^ISODate/', $item)) {
67
                $time = str_replace(['ISODate("','")'], '', $item);
68
                $dateTime = new \DateTime($time);
69
                $item = new UTCDatetime($dateTime->getTimestamp() * 1000);
70
            }
71
72 1
            $data[$key] = $this->walkAndConvertToUTCDatetime($item);
73
        }
74
75 2
        return $data;
76
    }
77
}