Skip to content

Commit 6624899

Browse files
committed
Added dataset import
1 parent d008f2c commit 6624899

File tree

7 files changed

+350
-1
lines changed

7 files changed

+350
-1
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"symfony/flex": "^2",
1717
"symfony/framework-bundle": "7.1.*",
1818
"symfony/runtime": "7.1.*",
19+
"symfony/validator": "7.1.*",
1920
"symfony/yaml": "7.1.*"
2021
},
2122
"require-dev": {

composer.lock

+176-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/packages/validator.yaml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
framework:
2+
validation:
3+
# Enables validator auto-mapping support.
4+
# For instance, basic validation constraints will be inferred from Doctrine's metadata.
5+
#auto_mapping:
6+
# App\Entity\: []
7+
8+
when@test:
9+
framework:
10+
validation:
11+
not_compromised_password: false

src/Domain/PowerStation.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace App\Domain;
4+
5+
use Symfony\Component\Validator\Constraints as Assert;
6+
7+
readonly class PowerStation
8+
{
9+
public function __construct(
10+
#[Assert\Range(min: -98.6, max: 56.7)]
11+
public float $temperature,
12+
13+
#[Assert\GreaterThan(value: 0)]
14+
public float $fumes,
15+
16+
#[Assert\Range(min: 870, max: 1084)]
17+
public float $pressure,
18+
19+
#[Assert\GreaterThan(value: 0)]
20+
#[Assert\LessThanOrEqual(value: 100)]
21+
public float $humidity,
22+
23+
#[Assert\GreaterThan(value: 0)]
24+
public float $power,
25+
) {
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace App\Infrastructure\Reader;
4+
5+
use App\Application\Cache\DatasetCacheInterface;
6+
use App\Application\Path\AppPathResolver;
7+
use App\Domain\PowerStation;
8+
use Doctrine\Common\Collections\ArrayCollection;
9+
use League\Csv\Reader;
10+
use Symfony\Component\Validator\Validator\ValidatorInterface;
11+
12+
readonly class PowerStationDatasetReader
13+
{
14+
public function __construct(
15+
private AppPathResolver $appPathResolver,
16+
private DatasetCacheInterface $datasetCache,
17+
private ValidatorInterface $validator,
18+
) {
19+
}
20+
21+
/**
22+
* @return ArrayCollection<string, ArrayCollection<int, PowerStation>>
23+
*/
24+
public function read(): ArrayCollection
25+
{
26+
return $this->datasetCache->get(
27+
sprintf('%s_dataset', self::class),
28+
function () {
29+
$dataset = new ArrayCollection();
30+
$valid = new ArrayCollection();
31+
$invalid = new ArrayCollection();
32+
$dataset->set('valid', $valid);
33+
$dataset->set('invalid', $invalid);
34+
35+
$data = Reader::createFromPath($this->appPathResolver->getResourcesPath('elektrownia.csv'));
36+
$data->setHeaderOffset(0);
37+
38+
foreach ($data as $row) {
39+
$powerStation = $this->createPowerStation($row);
40+
$errors = $this->validator->validate($powerStation);
41+
42+
if ($errors->count() === 0) {
43+
$valid->add($powerStation);
44+
} else {
45+
$invalid->add($powerStation);
46+
}
47+
}
48+
49+
return $dataset;
50+
}
51+
);
52+
}
53+
54+
/**
55+
* @param array<string, string> $data
56+
*/
57+
private function createPowerStation(array $data): PowerStation
58+
{
59+
return new PowerStation(
60+
temperature: round((float) ($data['Temperatura'] ?? 0.0), 2),
61+
fumes: round((float) ($data['Spaliny'] ?? 0.0), 2),
62+
pressure: round((float) ($data['Ciśnienie'] ?? 0.0), 2),
63+
humidity: round((float) ($data['Wilgotność'] ?? 0.0), 2),
64+
power: round((float) ($data['Moc'] ?? 0.0), 2),
65+
);
66+
}
67+
}

src/UI/CLI/SolveTaskCommand.php

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace App\UI\CLI;
4+
5+
use App\Infrastructure\Reader\PowerStationDatasetReader;
6+
use Symfony\Component\Console\Attribute\AsCommand;
7+
use Symfony\Component\Console\Command\Command;
8+
use Symfony\Component\Console\Input\InputInterface;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
use Symfony\Component\Console\Style\SymfonyStyle;
11+
use Symfony\Component\Validator\Validator\ValidatorInterface;
12+
13+
#[AsCommand(
14+
name: 'app:solve-task',
15+
)]
16+
class SolveTaskCommand extends Command
17+
{
18+
public function __construct(
19+
private readonly PowerStationDatasetReader $datasetReader,
20+
private readonly ValidatorInterface $validator,
21+
) {
22+
parent::__construct();
23+
}
24+
25+
protected function execute(InputInterface $input, OutputInterface $output): int
26+
{
27+
$io = new SymfonyStyle($input, $output);
28+
29+
$io->info('Dataset reading...');
30+
$dataset = $this->datasetReader->read();
31+
32+
$io->block([
33+
sprintf('Valid rows: %d', $dataset->get('valid')->count()),
34+
sprintf('Invalid rows: %d', $dataset->get('invalid')->count()),
35+
]);
36+
37+
if (!$dataset->get('invalid')->isEmpty()) {
38+
foreach ($dataset->get('invalid') as $invalid) {
39+
$firstError = $this->validator->validate($invalid)->get(0);
40+
41+
$io->warning(sprintf(
42+
'Invalid %s: %f - %s',
43+
$firstError->getPropertyPath(),
44+
$invalid->{$firstError->getPropertyPath()},
45+
$firstError->getMessage(),
46+
));
47+
}
48+
}
49+
50+
// TODO rest of task
51+
52+
$io->success('OK');
53+
54+
return Command::SUCCESS;
55+
}
56+
}

symfony.lock

+12
Original file line numberDiff line numberDiff line change
@@ -100,5 +100,17 @@
100100
"config/packages/routing.yaml",
101101
"config/routes.yaml"
102102
]
103+
},
104+
"symfony/validator": {
105+
"version": "7.1",
106+
"recipe": {
107+
"repo": "github.com/symfony/recipes",
108+
"branch": "main",
109+
"version": "7.0",
110+
"ref": "8c1c4e28d26a124b0bb273f537ca8ce443472bfd"
111+
},
112+
"files": [
113+
"config/packages/validator.yaml"
114+
]
103115
}
104116
}

0 commit comments

Comments
 (0)