Skip to content

Commit

Permalink
Remplace Material::format() par des accesseurs
Browse files Browse the repository at this point in the history
  • Loading branch information
Donov4n committed Jan 5, 2021
1 parent 8640b5e commit a7ab5d9
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 76 deletions.
2 changes: 0 additions & 2 deletions server/src/App/Controllers/EventController.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,6 @@ protected function _getFormattedEvent(int $id): array
->find($id);

$result = $model->toArray();
$result['materials'] = array_map([Material::class, 'format'], $result['materials']);

if (!$model->bills) {
return $result;
}
Expand Down
4 changes: 1 addition & 3 deletions server/src/App/Controllers/MaterialController.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ public function getAll(Request $request, Response $response): Response
$results = $results->withPath($basePath)->appends($params);
$results = $this->_formatPagination($results);

$results['data'] = array_map([Material::class, 'format'], $results['data']);

if ($whileEvent) {
$eventId = (int)$whileEvent;
$Event = new Event();
Expand Down Expand Up @@ -183,6 +181,6 @@ protected function _saveMaterial(?int $id, array $postData): array
}

$model = $this->model->find($result->id);
return Material::format($model->toArray());
return $model->toArray();
}
}
2 changes: 0 additions & 2 deletions server/src/App/Controllers/ParkController.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ public function getMaterials(Request $request, Response $response)
$materials->withPath($basePath);

$results = $this->_formatPagination($materials);
$results['data'] = array_map([Material::class, 'format'], $results['data']);

return $response->withJson($results);
}
}
2 changes: 0 additions & 2 deletions server/src/App/Controllers/TagController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ public function getMaterials(Request $request, Response $response): Response
$materials->withPath($basePath);

$results = $this->_formatPagination($materials);
$results['data'] = array_map([Material::class, 'format'], $results['data']);

return $response->withJson($results);
}
}
35 changes: 17 additions & 18 deletions server/src/App/Models/Material.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,22 @@ public function Events()
'note' => 'string',
];

public function getStockQuantityAttribute($value)
{
if (!$this->is_unitary) {
return $value;
}
return 0;
}

public function getOutOfOrderQuantityAttribute($value)
{
if (!$this->is_unitary) {
return $value;
}
return 0;
}

public function getParkAttribute()
{
$park = $this->Park()->first();
Expand Down Expand Up @@ -185,6 +201,7 @@ public function getEventsAttribute()
// -
// ------------------------------------------------------

//-
public function recalcQuantitiesForPeriod(
array $data,
string $start,
Expand Down Expand Up @@ -275,22 +292,4 @@ protected function _getMaterialFromEvent(int $materialId, array $event): array

return $eventMaterialIndex === false ? [] : $event['materials'][$eventMaterialIndex];
}

// ------------------------------------------------------
// -
// - Static methods
// -
// ------------------------------------------------------

public static function format(array $material): array
{
if (!$material['is_unitary']) {
return $material;
}

return array_replace($material, [
'stock_quantity' => 0,
'out_of_order_quantity' => 0,
]);
}
}
49 changes: 0 additions & 49 deletions server/tests/models/MaterialTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,53 +328,4 @@ public function testCreateMaterial(): void
unset($result->updated_at);
$this->assertEquals($expected, $result->toArray());
}

public function testFormat(): void
{
$getTestData = function ($data = []) {
$baseData = [
'id' => 8,
'name' => 'Analog Mixing Console Yamaha RM800',
'reference' => 'RM800',
'is_unitary' => false,
'park_id' => 1,
'category_id' => 1,
'rental_price' => 100.0,
'replacement_price' => 100.6,
'stock_quantity' => 2,
'out_of_order_quantity' => 1,
'tags' => [
['id' => 4, 'name' => 'old matos'],
['id' => 5, 'name' => 'vintage'],
],
'attributes' => [],
];
return array_replace($baseData, $data);
};

// - Test de base.
$this->assertSame($getTestData(), Material::format($getTestData()));

// - Test avec une gestion unitaire et quantités `null`.
$testData = $getTestData([
'is_unitary' => true,
'stock_quantity' => null,
'out_of_order_quantity' => null,
]);
$expected = $getTestData([
'is_unitary' => true,
'stock_quantity' => 0,
'out_of_order_quantity' => 0,
]);
$this->assertSame($expected, Material::format($testData));

// - Test avec une gestion unitaire et quantités non-`null`.
$testData = $getTestData(['is_unitary' => true]);
$expected = $getTestData([
'is_unitary' => true,
'stock_quantity' => 0,
'out_of_order_quantity' => 0,
]);
$this->assertSame($expected, Material::format($testData));
}
}

0 comments on commit a7ab5d9

Please sign in to comment.