openstackid-resources/app/Services/Model/Imp/SponsoredProjectService.php
smarcet 35fc57473c Sponsored Projects - Endpoints
POST /api/v1/sponsored-projects

payload

name
description
is_active

scope

REALM_URL/sponsored-projects/write

GET /api/v1/sponsored-projects

scope

REALM_URL/sponsored-projects/read

PUT /api/v1/sponsored-projects/{id}

payload

name
description
is_active

scope

REALM_URL/sponsored-projects/write

GET /api/v1/sponsored-projects/{id}
scope

REALM_URL/sponsored-projects/read

PUBLIC

GET /api/public/v1/sponsored-projects/{slug}

DELETE /api/v1/sponsored-projects/{id}

scope

REALM_URL/sponsored-projects/write

POST /api/v1/sponsored-projects/{id}/sponsorship-types

payload
name
description
is_active
order

scope

REALM_URL/sponsored-projects/write

GET /api/v1/sponsored-projects/{id}/sponsorship-types

scope

REALM_URL/sponsored-projects/read

GET /api/v1/sponsored-projects/{id}/sponsorship-types/{id}

scope

REALM_URL/sponsored-projects/read

DELETE /api/v1/sponsored-projects/{id}/sponsorship-types/{id}

scope

REALM_URL/sponsored-projects/write

PUT /api/v1/sponsored-projects/{id}/sponsorship-types/{id}

payload
name
description
is_active
order

scope

REALM_URL/sponsored-projects/write

PUT /api/v1/sponsored-projects/{id}/sponsorship-types/{id}/supporting-companies/{id}

payload
order (optional)

scope

REALM_URL/sponsored-projects/write

DELETE /api/v1/sponsored-projects/{id}/sponsorship-types/{id}/supporting-companies/{id}

scope

REALM_URL/sponsored-projects/write

GET /api/v1/sponsored-projects/{id}/sponsorship-types/{id}/supporting-companies

scope

REALM_URL/sponsored-projects/read

Change-Id: I9c0b1bb457a1c583afd284f56f2aced5deceaa02
Signed-off-by: smarcet <smarcet@gmail.com>
2020-11-30 15:09:40 -03:00

262 lines
11 KiB
PHP

<?php namespace App\Services\Model\Imp;
/**
* Copyright 2020 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use App\Models\Foundation\Main\Factories\ProjectSponsorshipTypeFactory;
use App\Models\Foundation\Main\Factories\SponsoredProjectFactory;
use App\Models\Foundation\Main\Repositories\ISponsoredProjectRepository;
use App\Services\Model\AbstractService;
use App\Services\Model\ISponsoredProjectService;
use libs\utils\ITransactionService;
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use models\main\Company;
use models\main\ICompanyRepository;
use models\main\ProjectSponsorshipType;
use models\main\SponsoredProject;
use models\main\SupportingCompany;
/**
* Class SponsoredProjectService
* @package App\Services\Model\Imp
*/
final class SponsoredProjectService
extends AbstractService
implements ISponsoredProjectService
{
/**
* @var ISponsoredProjectRepository
*/
private $repository;
/**
* @var ICompanyRepository
*/
private $company_repository;
/**
* SponsoredProjectService constructor.
* @param ISponsoredProjectRepository $repository
* @param ICompanyRepository $company_repository
* @param ITransactionService $tx_service
*/
public function __construct
(
ISponsoredProjectRepository $repository,
ICompanyRepository $company_repository,
ITransactionService $tx_service
)
{
parent::__construct($tx_service);
$this->repository = $repository;
$this->company_repository = $company_repository;
}
/**
* @inheritDoc
*/
public function add(array $payload): SponsoredProject
{
return $this->tx_service->transaction(function() use ($payload){
$name = trim($payload['name']);
$formerProject = $this->repository->getByName($name);
if(!is_null($formerProject)){
throw new ValidationException(sprintf("sponsored project %s already exists.", $name));
}
$sponsoredProject = SponsoredProjectFactory::build($payload);
$this->repository->add($sponsoredProject);
return $sponsoredProject;
});
}
/**
* @inheritDoc
*/
public function update(int $project_id, array $payload): SponsoredProject
{
return $this->tx_service->transaction(function() use ($project_id, $payload){
if(isset($payload['name'])) {
$name = trim($payload['name']);
$formerProject = $this->repository->getByName($name);
if (!is_null($formerProject) && $formerProject->getId() !== $project_id) {
throw new ValidationException(sprintf("sponsored project %s already exists.", $name));
}
}
$sponsoredProject = $this->repository->getById($project_id);
if(is_null($sponsoredProject) || !$sponsoredProject instanceof SponsoredProject)
throw new EntityNotFoundException(sprintf("sponsored project %s not found.", $project_id));
SponsoredProjectFactory::populate($sponsoredProject, $payload);
return $sponsoredProject;
});
}
/**
* @inheritDoc
*/
public function delete(int $project_id): void
{
$this->tx_service->transaction(function() use ($project_id){
$sponsoredProject = $this->repository->getById($project_id);
if(is_null($sponsoredProject) || !$sponsoredProject instanceof SponsoredProject)
throw new EntityNotFoundException(sprintf("sponsored project %s not found.", $project_id));
$this->repository->delete($sponsoredProject);
});
}
/**
* @inheritDoc
*/
public function addProjectSponsorshipType(int $project_id, array $payload): ProjectSponsorshipType
{
return $this->tx_service->transaction(function() use ($project_id, $payload){
$sponsoredProject = $this->repository->getById($project_id);
if(is_null($sponsoredProject) || !$sponsoredProject instanceof SponsoredProject)
throw new EntityNotFoundException(sprintf("sponsored project %s not found.", $project_id));
$name = trim($payload['name']);
if($sponsoredProject->getSponsorshipTypeByName($name)){
throw new ValidationException(sprintf("sponsorship type %s already exists.", $name));
}
$projectSponsorshipType = ProjectSponsorshipTypeFactory::build($payload);
$sponsoredProject->addSponsorshipType($projectSponsorshipType);
return $projectSponsorshipType;
});
}
/**
* @inheritDoc
*/
public function updateProjectSponsorshipType(int $project_id, int $sponsorship_id, array $payload): ProjectSponsorshipType
{
return $this->tx_service->transaction(function() use ($project_id, $sponsorship_id, $payload){
$sponsoredProject = $this->repository->getById($project_id);
if(is_null($sponsoredProject) || !$sponsoredProject instanceof SponsoredProject)
throw new EntityNotFoundException(sprintf("sponsored project %s not found.", $project_id));
if(isset($payload['name'])) {
$name = trim($payload['name']);
$formerProjectSponsorshipType = $sponsoredProject->getSponsorshipTypeByName($name);
if ($formerProjectSponsorshipType && $formerProjectSponsorshipType->getId() !== $sponsorship_id) {
throw new ValidationException(sprintf("sponsorship type %s already exists.", $name));
}
}
$projectSponsorshipType = $sponsoredProject->getSponsorshipTypeById($sponsorship_id);
if(is_null($projectSponsorshipType) || !$projectSponsorshipType instanceof ProjectSponsorshipType)
throw new EntityNotFoundException(sprintf("sponsorship type %s not found.", $project_id));
$projectSponsorshipType = ProjectSponsorshipTypeFactory::populate($projectSponsorshipType, $payload);
if (isset($payload['order']) && intval($payload['order']) != $projectSponsorshipType->getOrder()) {
// request to update order
$sponsoredProject->recalculateProjectSponsorshipTypeOrder($projectSponsorshipType, intval($payload['order']));
}
return $projectSponsorshipType;
});
}
/**
* @param int $project_id
* @param int $sponsorship_id
* @throws \Exception
*/
public function deleteProjectSponsorshipType(int $project_id, int $sponsorship_id): void
{
$this->tx_service->transaction(function() use ($project_id, $sponsorship_id){
$sponsoredProject = $this->repository->getById($project_id);
if(is_null($sponsoredProject) || !$sponsoredProject instanceof SponsoredProject)
throw new EntityNotFoundException(sprintf("sponsored project %s not found.", $project_id));
$projectSponsorshipType = $sponsoredProject->getSponsorshipTypeById($sponsorship_id);
if(is_null($projectSponsorshipType) || !$projectSponsorshipType instanceof ProjectSponsorshipType)
throw new EntityNotFoundException(sprintf("sponsorship type %s not found.", $project_id));
$sponsoredProject->removeSponsorshipType($projectSponsorshipType);
});
}
/**
* @param int $project_id
* @param int $sponsorship_id
* @param int $company_id
* @param array $payload
* @return SupportingCompany
* @throws \Exception
*/
public function addCompanyToProjectSponsorshipType(int $project_id, int $sponsorship_id, int $company_id, array $payload): SupportingCompany
{
return $this->tx_service->transaction(function() use ($project_id, $sponsorship_id, $company_id, $payload){
$sponsoredProject = $this->repository->getById($project_id);
if(is_null($sponsoredProject) || !$sponsoredProject instanceof SponsoredProject)
throw new EntityNotFoundException(sprintf("sponsored project %s not found.", $project_id));
$projectSponsorshipType = $sponsoredProject->getSponsorshipTypeById($sponsorship_id);
if(is_null($projectSponsorshipType) || !$projectSponsorshipType instanceof ProjectSponsorshipType)
throw new EntityNotFoundException(sprintf("sponsorship type %s not found.", $project_id));
$company = $this->company_repository->getById($company_id);
if(is_null($company) || !$company instanceof Company)
throw new EntityNotFoundException(sprintf("company %s not found.", $company_id));
$supportingCompany = $projectSponsorshipType->addSupportingCompany($company);
if (isset($payload['order']) && intval($payload['order']) != $supportingCompany->getOrder()) {
// request to update order
$projectSponsorshipType->recalculateSupportingCompanyOrder($supportingCompany, intval($payload['order']));
}
return $supportingCompany;
});
}
/**
* @inheritDoc
*/
public function removeCompanyToProjectSponsorshipType(int $project_id, int $sponsorship_id, int $company_id): void
{
$this->tx_service->transaction(function() use ($project_id, $sponsorship_id, $company_id){
$sponsoredProject = $this->repository->getById($project_id);
if(is_null($sponsoredProject) || !$sponsoredProject instanceof SponsoredProject)
throw new EntityNotFoundException(sprintf("sponsored project %s not found.", $project_id));
$projectSponsorshipType = $sponsoredProject->getSponsorshipTypeById($sponsorship_id);
if(is_null($projectSponsorshipType) || !$projectSponsorshipType instanceof ProjectSponsorshipType)
throw new EntityNotFoundException(sprintf("sponsorship type %s not found.", $project_id));
$company = $this->company_repository->getById($company_id);
if(is_null($company) || !$company instanceof Company)
throw new EntityNotFoundException(sprintf("company %s not found.", $company_id));
$projectSponsorshipType->removeSupportingCompany($company);
});
}
}