Completed
Push — develop ( b15e90...d426db )
by Alejandro
16s queued 12s
created

data/migrations/Version20180913205455.php (2 issues)

1
<?php
2
3
declare(strict_types=1);
4
5
namespace ShlinkMigrations;
6
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\Schema\Schema;
9
use Doctrine\Migrations\AbstractMigration;
10
use PDO;
11
use Shlinkio\Shlink\Common\Exception\InvalidArgumentException;
12
use Shlinkio\Shlink\Common\Util\IpAddress;
13
14
/**
15
 * Auto-generated Migration: Please modify to your needs!
16
 */
17
final class Version20180913205455 extends AbstractMigration
18
{
19
    /**
20
     */
21
    public function up(Schema $schema): void
22
    {
23
        // Nothing to create
24
    }
25
26
    /**
27
     * @throws DBALException
28
     */
29
    public function postUp(Schema $schema): void
30
    {
31
        $qb = $this->connection->createQueryBuilder();
32
        $qb->select('id', 'remote_addr')
33
           ->from('visits');
34
        $st = $this->connection->executeQuery($qb->getSQL());
35
36
        $qb = $this->connection->createQueryBuilder();
37
        $qb->update('visits', 'v')
38
           ->set('v.remote_addr', ':obfuscatedAddr')
39
           ->where('v.id=:id');
40
41
        while ($row = $st->fetch(PDO::FETCH_ASSOC)) {
0 ignored issues
show
Deprecated Code introduced by
The function Doctrine\DBAL\Driver\ResultStatement::fetch() has been deprecated: Use fetchNumeric(), fetchAssociative() or fetchOne() instead. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

41
        while ($row = /** @scrutinizer ignore-deprecated */ $st->fetch(PDO::FETCH_ASSOC)) {

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
42
            $addr = $row['remote_addr'] ?? null;
43
            if ($addr === null) {
44
                continue;
45
            }
46
47
            $qb->setParameters([
48
                'id' => $row['id'],
49
                'obfuscatedAddr' => $this->determineAddress((string) $addr),
50
            ])->execute();
51
        }
52
    }
53
54
    private function determineAddress(string $addr): ?string
55
    {
56
        if ($addr === IpAddress::LOCALHOST) {
57
            return $addr;
58
        }
59
60
        try {
61
            return (string) IpAddress::fromString($addr)->getObfuscatedCopy();
0 ignored issues
show
Deprecated Code introduced by
The function Shlinkio\Shlink\Common\U...ss::getObfuscatedCopy() has been deprecated: Use getAnonymizedCopy instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

61
            return (string) /** @scrutinizer ignore-deprecated */ IpAddress::fromString($addr)->getObfuscatedCopy();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
62
        } catch (InvalidArgumentException $e) {
63
            return null;
64
        }
65
    }
66
67
    /**
68
     */
69
    public function down(Schema $schema): void
70
    {
71
        // Nothing to rollback
72
    }
73
}
74