CharacterCrawler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 5
dl 0
loc 46
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A crawl() 0 5 1
A removeCharacters() 0 4 1
B fetchCharacters() 0 25 4
1
<?php
2
3
namespace App\Utils;
4
5
use App\Entity\Character;
6
use App\Entity\Guild;
7
use App\Entity\Setting;
8
use App\Entity\User;
9
use App\Factory\CharacterFactory;
10
use App\Factory\UserFactory;
11
use Symfony\Component\DomCrawler\Crawler;
12
13
/**
14
 * Class CharacterCrawler
15
 * @package App\Utils
16
 */
17
class CharacterCrawler extends AbstractCrawler
18
{
19
    /**
20
     * @param Setting $settings
21
     */
22
    public function crawl(Setting $settings)
23
    {
24
        $crawler = new Crawler($this->getSiteHtml($settings->getApi()));
25
        $this->fetchCharacters($crawler->filter('li.character'));
26
    }
27
28
    private function removeCharacters(): void
29
    {
30
        $this->em->getRepository(Character::class)->remove();
31
    }
32
33
    /**
34
     * @param Guild $guild
0 ignored issues
show
Bug introduced by
There is no parameter named $guild. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
35
     * @param array $domList
0 ignored issues
show
Bug introduced by
There is no parameter named $domList. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
36
     */
37
    private function fetchCharacters( $dom): void
38
    {
39
        $this->removeCharacters();
40
        /** @var \DOMElement $item */
41
        foreach ($dom as $key => $domElement) {
42
            if ($key > 1) {
43
                $html = $domElement->ownerDocument->saveHTML($domElement);
44
                $liCrawler = new Crawler($html);
45
46
                $side = explode("·", $liCrawler->filter('.media-heading > small > span')->html());
47
                preg_match("/\/characters\/(.*)\//", $liCrawler->filter('a')->getNode(0)->getAttribute('href'), $matches);
48
49
                $character = CharacterFactory::create([
50
                    'code' => $matches[1],
51
                    'name' => $domElement->getAttribute('data-name-lower'),
52
                    'description' => strip_tags($liCrawler->filter('p.character-description')->html()),
53
                    'side' => 'Light Side' === trim($side[0]) ? 1 : 0,
54
                    'image' => $liCrawler->filter('img.char-portrait-img')->getNode(0)->getAttribute('src'),
55
                ]);
56
57
                $this->em->persist($character);
58
            }
59
        }
60
        $this->em->flush();
61
    }
62
}
63