<?php declare(strict_types=1); /* * This file is part of dacore bundle for Contao. * * (c) Benjamin Roth * * @license commercial */ namespace vossmedien\DacoreBundle\API; use Contao\System; use Symfony\Component\Mime\Part\DataPart; use Symfony\Component\Mime\Part\Multipart\FormDataPart; use Symfony\Component\Serializer\SerializerInterface; use Symfony\Contracts\HttpClient\HttpClientInterface; use vossmedien\DacoreBundle\Model\KatalogModel; use vossmedien\DacoreBundle\Model\StelleModel; use vossmedien\DacoreBundle\Model\StellenModel; class Softgarden { /** * @var SerializerInterface */ protected $serializer; /** * @var HttpClientInterface */ protected $httpClient; protected $clientId; protected $apiDomain; public function __construct(SerializerInterface $serializer, HttpClientInterface $httpClient) { $this->serializer = $serializer; $this->httpClient = $httpClient; $this->clientId = System::getContainer()->getParameter('vossmedien_dacore.softgarden.client_id'); $this->apiDomain = System::getContainer()->getParameter('vossmedien_dacore.softgarden.api_domain'); } protected function getClientId() { return $this->clientId; } protected function getApiDomain() { return rtrim($this->apiDomain,'/'); } protected function getBasicAuthorization() { return [$this->getClientId(),'']; } protected function sendRequest(string $relEndpoint, array $options, string $method = 'GET',bool $blnFQDNEndpoint=false) { if ($blnFQDNEndpoint) { return $this->httpClient->request($method,$relEndpoint,$options); } $relEndpoint = '/' . ltrim($relEndpoint,'/'); return $this->httpClient->request($method,$this->getApiDomain().$relEndpoint,$options); } public function testApiRequest() { // bewerber@dacore.api / Aebekai2ail4coum $options = [ 'auth_basic' => $this->getBasicAuthorization(), 'headers' => ['Content-Type: application/json'] ]; $response = $this->sendRequest('jobslist/102581_extern',$options); if ($response->getStatusCode() == 200) { $content = $response->getContent(); return json_decode($content); } return null; } public function hasApplied(int $jobId, $token_type, $access_token) { // bewerber@dacore.api / Aebekai2ail4coum $options = [ 'headers' => [ 'Authorization: ' . $token_type . ' ' . $access_token ] ]; $response = $this->sendRequest('jobs/' . $jobId . '/applied',$options); if ($response->getStatusCode() == 200) { $content = $response->getContent(); return $content == 'true'; } return false; } public function getApplicationId(int $jobId, $token_type, $access_token) { // bewerber@dacore.api / Aebekai2ail4coum $options = [ 'headers' => [ 'Authorization: ' . $token_type . ' ' . $access_token, 'Content-Type: application/json' ] ]; $response = $this->sendRequest('jobs/' . $jobId . '/application',$options); if ($response->getStatusCode() == 200) { $content = $response->getContent(); return $content; } return null; } public function startApplication(int $jobId, $token_type, $access_token) { // bewerber@dacore.api / Aebekai2ail4coum $options = [ 'headers' => [ 'Authorization: ' . $token_type . ' ' . $access_token, 'Content-Type: application/json' ] ]; $response = $this->sendRequest('applications?jobId='.$jobId,$options, 'POST'); if ($response->getStatusCode() == 200) { $content = $response->getContent(); return $content; } return null; } public function getApplication(string $applicationId, $token_type, $access_token) { // bewerber@dacore.api / Aebekai2ail4coum $options = [ 'headers' => [ 'Authorization: ' . $token_type . ' ' . $access_token, 'Content-Type: application/json' ] ]; $response = $this->sendRequest('applications/' . $applicationId . '?fields=*',$options); if ($response->getStatusCode() == 200) { $content = json_decode($response->getContent()); return $content; } return null; } public function deleteApplication(string $applicationId, $token_type, $access_token) { // bewerber@dacore.api / Aebekai2ail4coum $options = [ 'headers' => [ 'Authorization: ' . $token_type . ' ' . $access_token, 'Content-Type: application/json' ] ]; $response = $this->sendRequest('applications/' . $applicationId,$options,'DELETE'); if ($response->getStatusCode() == 204) { return true; } return false; } public function createApplicantUser(string $username,string $password,?string $email=null) { if ($email === null) { $email = $username; } $options = [ 'auth_basic' => $this->getBasicAuthorization(), 'headers' => [ 'Content-Type: application/json' ], 'body' => json_encode([ 'salutation' => 0, 'firstname' => 'Dacore', 'lastname' => 'Bewerber', 'username' => $username, 'password' => $password, 'email' => $email, 'locale' => 'de', 'dataPrivacyAccepted' => true ]) ]; $response = $this->sendRequest('applicants',$options,'POST'); if ($response->getStatusCode() == 204) { return true; } return false; } public function addApplicationInfo(array $data, string $applicationId, $token_type, $access_token) { $options = [ 'headers' => [ 'Authorization: ' . $token_type . ' ' . $access_token, 'Content-Type: application/json' ], 'body' => json_encode($data) ]; $response = $this->sendRequest('applications/' . $applicationId,$options,'POST'); if ($response->getStatusCode() == 204) { return true; } return false; } public function addApplicationFileCV(string $path, string $filename, string $mimetype, string $applicationId, $token_type, $access_token) { $fields = [ 'file' => DataPart::fromPath($path,$filename,$mimetype) ]; $formData = new FormDataPart($fields); $options = [ 'headers' => array_merge([ 'Authorization: ' . $token_type . ' ' . $access_token ], $formData->getPreparedHeaders()->toArray() ), 'body' => $formData->bodyToString() ]; $response = $this->sendRequest('attachments/cv?applicationId=' . $applicationId,$options,'POST'); if ($response->getStatusCode() == 200) { return true; } return false; } public function addApplicationFile(string $path, string $filename, string $mimetype, string $applicationId, $token_type, $access_token) { $fields = [ 'file' => DataPart::fromPath($path,$filename,$mimetype) ]; $formData = new FormDataPart($fields); $options = [ 'headers' => array_merge([ 'Authorization: ' . $token_type . ' ' . $access_token ], $formData->getPreparedHeaders()->toArray() ), 'body' => $formData->bodyToString() ]; $response = $this->sendRequest('attachments/?applicationId=' . $applicationId,$options,'POST'); if ($response->getStatusCode() == 200) { return true; } return false; } public function getUserAccessToken($username, $password) { // bewerber@dacore.api / Aebekai2ail4coum $options = [ 'auth_basic' => $this->getBasicAuthorization(), 'body' => [ 'grant_type' => 'password', 'username' => $username, 'password' => $password ] ]; $response = $this->sendRequest('https://api.softgarden.io/api/rest/oauth/frontend/token',$options,'POST',true); if ($response->getStatusCode() == 200) { $content = $response->getContent(); return json_decode($content); } return null; } public function finalizeApplication(string $applicationId, $token_type, $access_token) { $options = [ 'headers' => [ 'Authorization: ' . $token_type . ' ' . $access_token, 'Content-Type: application/json' ] ]; $response = $this->sendRequest('applications/' . $applicationId . '/submit',$options,'POST'); if ($response->getStatusCode() == 204) { return true; } return false; } }