Symfony Class / Service to check / get or create new object in database

Hello i have one logic problem.

    public function addExternalApiSystem(
        string $systemName,
        Request $request,
        ExternalApiSystemManage $systemManage
    )
    {
        if (isset(systemName)) {
            $system = $systemManage->manage($systemName);
        }

        return $this->json(['system' => system], 200);
    }

And here is my ExternalSystemManage class

class ExternalApiSystemManage
{
    /**
     * @var ExternalApiSystemRepository
     */
    private ExternalApiSystemRepository $apiSystemRepository;
    /**
     * @var ExternalApiSystemRegisterer
     */
    private ExternalApiSystemRegisterer $apiSystemRegisterer;

    public function __construct(
        ExternalApiSystemRepository $apiSystemRepository,
        ExternalApiSystemRegisterer $apiSystemRegisterer
    )
    {
        $this->apiSystemRepository = $apiSystemRepository;
        $this->apiSystemRegisterer = $apiSystemRegisterer;
    }

    public function manage(string $systemName) : ExternalApiSystem
    {
        $system = $this->apiSystemRepository->findOneBy([
            'name' => $systemName
        ]);

        return $system ?? $this->apiSystemRegisterer->register($systemName);
    }
}

Register method:

    public function register(string $apiSystemName)
    {
        $apiSystem = $this->factory->create($apiSystemName);
        $this->apiSystemRepository->save($apiSystem);

        return $apiSystem;
    }

And the question is
Is this good solution of this problem?
I need method which will return me object of ExternalSystem. If ExternalSystem doesnt exist in database will create new one and return it.

Did i use properly nomenclature?

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *