Truncate   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 3
dl 0
loc 44
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B truncateEntity() 0 32 4
1
<?php
2
3
namespace Rottenwood\KingdomBundle\Command\Console\Integration;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Output\OutputInterface;
7
8
/** {@inheritDoc} */
9
abstract class Truncate extends ContainerAwareCommand
10
{
11
12
    /**
13
     * @param string          $entityName
14
     * @param OutputInterface $output
15
     * @param string          $startEcho
16
     * @param string          $endEcho
17
     * @throws \Doctrine\DBAL\ConnectionException
18
     * @throws \Doctrine\DBAL\DBALException
19
     */
20
    protected function truncateEntity(
21
        string $entityName,
22
        OutputInterface $output = null,
23
        string $startEcho = '',
24
        string $endEcho = ''
25
    ) {
26
        if ($output) {
27
            $output->write($startEcho);
28
        }
29
30
        $em = $this->getContainer()->get('doctrine.orm.entity_manager');
31
32
        $entityMetadata = $em->getClassMetadata($entityName);
33
        $tableName = $entityMetadata->getTableName();
34
        $connection = $em->getConnection();
35
        $connection->beginTransaction();
36
37
        try {
38
            $connection->query('SET FOREIGN_KEY_CHECKS=0');
39
            $connection->query('DELETE FROM ' . $tableName);
40
            $connection->query('SET FOREIGN_KEY_CHECKS=1');
41
            $connection->commit();
42
        } catch (\Exception $e) {
43
            $connection->rollback();
44
        }
45
46
        $connection->exec('ALTER TABLE ' . $tableName . ' AUTO_INCREMENT = 1;');
47
48
        if ($output) {
49
            $output->writeln($endEcho);
50
        }
51
    }
52
}
53