Failed Conditions
Pull Request — master (#6691)
by Dariusz
23:59 queued 15:29
created

LockAgentWorker::createEntityManager()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 13
nc 1
nop 1
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 10 and the first side effect is on line 118.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Doctrine\Tests\ORM\Functional\Locking;
4
5
use Doctrine\Common\Cache\ArrayCache;
6
use Doctrine\DBAL\Logging\EchoSQLLogger;
7
use Doctrine\ORM\Configuration;
8
use Doctrine\ORM\EntityManager;
9
10
class LockAgentWorker
11
{
12
    private $em;
13
14
    static public function run()
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
15
    {
16
        $lockAgent = new LockAgentWorker();
17
18
        $worker = new \GearmanWorker();
19
        $worker->addServer(
20
            $_SERVER['GEARMAN_HOST'] ?? null,
21
            $_SERVER['GEARMAN_PORT'] ?? 4730
22
        );
23
        $worker->addFunction("findWithLock", [$lockAgent, "findWithLock"]);
24
        $worker->addFunction("dqlWithLock", [$lockAgent, "dqlWithLock"]);
25
        $worker->addFunction('lock', [$lockAgent, 'lock']);
26
27
        while($worker->work()) {
28
            if ($worker->returnCode() != GEARMAN_SUCCESS) {
29
                echo "return_code: " . $worker->returnCode() . "\n";
30
                break;
31
            }
32
        }
33
    }
34
35
    protected function process($job, \Closure $do)
36
    {
37
        $fixture = $this->processWorkload($job);
38
39
        $s = microtime(true);
40
        $this->em->beginTransaction();
41
        $do($fixture, $this->em);
42
43
        sleep(1);
44
        $this->em->rollback();
45
        $this->em->clear();
46
        $this->em->close();
47
        $this->em->getConnection()->close();
48
49
        return (microtime(true) - $s);
50
    }
51
52
    public function findWithLock($job)
53
    {
54
        return $this->process($job, function($fixture, $em) {
55
            $entity = $em->find($fixture['entityName'], $fixture['entityId'], $fixture['lockMode']);
0 ignored issues
show
Unused Code introduced by
$entity is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
56
        });
57
    }
58
59
    public function dqlWithLock($job)
60
    {
61
        return $this->process($job, function($fixture, $em) {
62
            /* @var $query Doctrine\ORM\Query */
63
            $query = $em->createQuery($fixture['dql']);
64
            $query->setLockMode($fixture['lockMode']);
65
            $query->setParameters($fixture['dqlParams']);
66
            $result = $query->getResult();
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
67
        });
68
    }
69
70
    public function lock($job)
71
    {
72
        return $this->process($job, function($fixture, $em) {
73
            $entity = $em->find($fixture['entityName'], $fixture['entityId']);
74
            $em->lock($entity, $fixture['lockMode']);
75
        });
76
    }
77
78
    protected function processWorkload($job)
79
    {
80
        echo "Received job: " . $job->handle() . " for function " . $job->functionName() . "\n";
81
82
        $workload = $job->workload();
83
        $workload = unserialize($workload);
84
85
        if (!isset($workload['conn']) || !is_array($workload['conn'])) {
86
            throw new \InvalidArgumentException("Missing Database parameters");
87
        }
88
89
        $this->em = $this->createEntityManager($workload['conn']);
90
91
        if (!isset($workload['fixture'])) {
92
            throw new \InvalidArgumentException("Missing Fixture parameters");
93
        }
94
        return $workload['fixture'];
95
    }
96
97
    protected function createEntityManager($conn)
98
    {
99
        $config = new Configuration();
100
        $config->setProxyDir(__DIR__ . '/../../../Proxies');
101
        $config->setProxyNamespace('MyProject\Proxies');
102
        $config->setAutoGenerateProxyClasses(true);
103
104
        $annotDriver = $config->newDefaultAnnotationDriver([__DIR__ . '/../../../Models/'], true);
105
        $config->setMetadataDriverImpl($annotDriver);
106
107
        $cache = new ArrayCache();
108
        $config->setMetadataCacheImpl($cache);
109
        $config->setQueryCacheImpl($cache);
110
        $config->setSQLLogger(new EchoSQLLogger());
111
112
        $em = EntityManager::create($conn, $config);
113
114
        return $em;
115
    }
116
}
117
118
LockAgentWorker::run();
119