2019-03-23 17:47:20 -07:00
< ? php
2024-03-12 22:39:16 -04:00
namespace App\Http\Controllers\Api\Client\Servers ;
2019-03-23 17:47:20 -07:00
2025-09-24 13:34:19 +02:00
use App\Exceptions\DisplayException ;
2025-09-08 13:12:33 -04:00
use App\Exceptions\Model\DataValidationException ;
2024-03-12 22:39:16 -04:00
use App\Facades\Activity ;
use App\Http\Controllers\Api\Client\ClientApiController ;
2025-09-24 13:34:19 +02:00
use App\Http\Requests\Api\Client\Servers\Network\DeleteAllocationRequest ;
2024-03-12 22:39:16 -04:00
use App\Http\Requests\Api\Client\Servers\Network\GetNetworkRequest ;
use App\Http\Requests\Api\Client\Servers\Network\NewAllocationRequest ;
use App\Http\Requests\Api\Client\Servers\Network\SetPrimaryAllocationRequest ;
2025-09-24 13:34:19 +02:00
use App\Http\Requests\Api\Client\Servers\Network\UpdateAllocationRequest ;
use App\Models\Allocation ;
use App\Models\Server ;
use App\Services\Allocations\FindAssignableAllocationService ;
use App\Transformers\Api\Client\AllocationTransformer ;
2025-02-26 16:12:19 +01:00
use Dedoc\Scramble\Attributes\Group ;
2025-09-24 13:34:19 +02:00
use Illuminate\Http\JsonResponse ;
2019-03-23 17:47:20 -07:00
2025-02-26 16:12:19 +01:00
#[Group('Server - Allocation')]
2020-07-09 20:36:08 -07:00
class NetworkAllocationController extends ClientApiController
2019-03-23 17:47:20 -07:00
{
/**
2022-10-14 10:59:20 -06:00
* NetworkAllocationController constructor .
2020-09-28 16:14:14 -04:00
*/
2020-07-09 19:56:46 -07:00
public function __construct (
2022-10-14 10:59:20 -06:00
private FindAssignableAllocationService $assignableAllocationService ,
2020-07-09 19:56:46 -07:00
) {
2019-03-23 17:47:20 -07:00
parent :: __construct ();
}
/**
2025-02-26 16:12:19 +01:00
* List allocations
*
2022-10-14 10:59:20 -06:00
* Lists all the allocations available to a server and whether
* they are currently assigned as the primary for this server .
2025-03-03 14:41:19 -05:00
*
* @ return array < array - key , mixed >
2019-03-23 17:47:20 -07:00
*/
2019-09-05 21:41:20 -07:00
public function index ( GetNetworkRequest $request , Server $server ) : array
2019-03-23 17:47:20 -07:00
{
2020-07-09 19:56:46 -07:00
return $this -> fractal -> collection ( $server -> allocations )
-> transformWith ( $this -> getTransformer ( AllocationTransformer :: class ))
-> toArray ();
}
2020-07-09 20:36:08 -07:00
/**
2025-02-26 16:12:19 +01:00
* Update allocation
*
2020-07-09 20:36:08 -07:00
* Set the primary allocation for a server .
*
2025-03-03 14:41:19 -05:00
* @ return array < array - key , mixed >
*
2025-09-08 13:12:33 -04:00
* @ throws DataValidationException
2020-07-09 20:36:08 -07:00
*/
public function update ( UpdateAllocationRequest $request , Server $server , Allocation $allocation ) : array
{
2022-05-29 19:26:28 -04:00
$original = $allocation -> notes ;
$allocation -> forceFill ([ 'notes' => $request -> input ( 'notes' )]) -> save ();
if ( $original !== $allocation -> notes ) {
Activity :: event ( 'server:allocation.notes' )
-> subject ( $allocation )
2025-06-04 22:13:59 +02:00
-> property ([ 'allocation' => $allocation -> address , 'old' => $original , 'new' => $allocation -> notes ])
2022-05-29 19:26:28 -04:00
-> log ();
}
2020-07-09 20:36:08 -07:00
return $this -> fractal -> item ( $allocation )
-> transformWith ( $this -> getTransformer ( AllocationTransformer :: class ))
-> toArray ();
}
2020-07-09 19:56:46 -07:00
/**
2025-03-03 14:41:19 -05:00
* Set primary allocation
2025-02-26 16:12:19 +01:00
*
2020-07-09 19:56:46 -07:00
* Set the primary allocation for a server .
*
2025-03-03 14:41:19 -05:00
* @ return array < array - key , mixed >
*
2025-09-08 13:12:33 -04:00
* @ throws DataValidationException
2020-07-09 20:36:08 -07:00
*/
public function setPrimary ( SetPrimaryAllocationRequest $request , Server $server , Allocation $allocation ) : array
{
2024-03-16 15:01:21 -04:00
$server -> allocation () -> associate ( $allocation );
$server -> save ();
2020-07-09 20:36:08 -07:00
2022-05-29 19:26:28 -04:00
Activity :: event ( 'server:allocation.primary' )
-> subject ( $allocation )
2025-06-04 22:13:59 +02:00
-> property ( 'allocation' , $allocation -> address )
2022-05-29 19:26:28 -04:00
-> log ();
2020-09-28 11:50:34 -04:00
return $this -> fractal -> item ( $allocation )
-> transformWith ( $this -> getTransformer ( AllocationTransformer :: class ))
-> toArray ();
}
/**
2025-02-26 16:12:19 +01:00
* Create allocation
*
2020-09-28 11:50:34 -04:00
* Set the notes for the allocation for a server .
2020-10-31 14:58:15 -07:00
*
2025-03-03 14:41:19 -05:00
* @ return array < array - key , mixed >
*
2025-09-08 13:12:33 -04:00
* @ throws DisplayException
2020-09-28 11:50:34 -04:00
*/
2020-10-31 14:58:15 -07:00
public function store ( NewAllocationRequest $request , Server $server ) : array
2020-09-28 11:50:34 -04:00
{
2026-01-13 08:39:50 +01:00
$allocation = Activity :: event ( 'server:allocation.create' ) -> transaction ( function ( $log ) use ( $server ) {
$server -> allocations () -> lockForUpdate ();
2020-10-31 21:57:27 -07:00
2026-07-09 09:55:11 -04:00
throw_if ( $server -> allocations -> count () >= $server -> allocation_limit , new DisplayException ( 'Cannot assign additional allocations to this server: limit has been reached.' ));
2020-09-28 11:50:34 -04:00
2026-01-13 08:39:50 +01:00
$allocation = $this -> assignableAllocationService -> handle ( $server );
$log -> subject ( $allocation ) -> property ( 'allocation' , $allocation -> address );
return $allocation ;
});
2022-05-29 19:26:28 -04:00
2020-07-09 20:36:08 -07:00
return $this -> fractal -> item ( $allocation )
-> transformWith ( $this -> getTransformer ( AllocationTransformer :: class ))
-> toArray ();
}
/**
2025-02-26 16:12:19 +01:00
* Delete allocation
*
2020-07-09 20:36:08 -07:00
* Delete an allocation from a server .
*
2025-09-08 13:12:33 -04:00
* @ throws DisplayException
2020-07-09 19:56:46 -07:00
*/
2022-10-14 10:59:20 -06:00
public function delete ( DeleteAllocationRequest $request , Server $server , Allocation $allocation ) : JsonResponse
2020-07-09 19:56:46 -07:00
{
2022-05-07 15:05:28 -04:00
// Don't allow the deletion of allocations if the server does not have an
// allocation limit set.
2026-07-09 09:55:11 -04:00
throw_if ( empty ( $server -> allocation_limit ), new DisplayException ( 'You cannot delete allocations for this server: no allocation limit is set.' ));
2022-05-07 15:05:28 -04:00
2020-10-31 21:22:44 -07:00
Allocation :: query () -> where ( 'id' , $allocation -> id ) -> update ([
'notes' => null ,
'server_id' => null ,
]);
2019-03-23 17:47:20 -07:00
2022-05-29 19:26:28 -04:00
Activity :: event ( 'server:allocation.delete' )
-> subject ( $allocation )
2025-06-04 22:13:59 +02:00
-> property ( 'allocation' , $allocation -> address )
2022-05-29 19:26:28 -04:00
-> log ();
2020-07-09 20:36:08 -07:00
return new JsonResponse ([], JsonResponse :: HTTP_NO_CONTENT );
2019-03-23 17:47:20 -07:00
}
}