|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace BitBag\SyliusUserComPlugin\Api; |
| 6 | + |
| 7 | +use BitBag\SyliusUserComPlugin\Entity\UserComApiAwareInterface; |
| 8 | +use Symfony\Component\HttpFoundation\Request; |
| 9 | + |
| 10 | +final class UserApi extends AbstractClient implements UserApiInterface |
| 11 | +{ |
| 12 | + const PARENT = 'parent'; |
| 13 | + const USERS_LIST = 'users_list'; |
| 14 | + |
| 15 | + public function findUser( |
| 16 | + UserComApiAwareInterface $resource, |
| 17 | + string $value, |
| 18 | + string $field, |
| 19 | + ): ?array { |
| 20 | + $url = $this->getApiEndpointUrl( |
| 21 | + $resource, |
| 22 | + self::FIND_USER_ENDPOINT, |
| 23 | + sprintf('?%s=%s', $field, $value), |
| 24 | + ); |
| 25 | + |
| 26 | + return $this->request( |
| 27 | + $url, |
| 28 | + Request::METHOD_GET, |
| 29 | + $this->buildOptions($resource), |
| 30 | + ); |
| 31 | + } |
| 32 | + |
| 33 | + public function getUser(UserComApiAwareInterface $resource, int $userId): ?array |
| 34 | + { |
| 35 | + $url = $this->getApiEndpointUrl($resource, sprintf(self::GET_USER_ENDPOINT, $userId)); |
| 36 | + |
| 37 | + return $this->request( |
| 38 | + $url, |
| 39 | + Request::METHOD_GET, |
| 40 | + $this->buildOptions($resource), |
| 41 | + ); |
| 42 | + } |
| 43 | + |
| 44 | + public function updateOrCreateUser(UserComApiAwareInterface $resource, array $data): ?array |
| 45 | + { |
| 46 | + return $this->request( |
| 47 | + $this->getApiEndpointUrl($resource, self::UPDATE_OR_CREATE_USER_ENDPOINT), |
| 48 | + Request::METHOD_POST, |
| 49 | + $this->buildOptions($resource, [ |
| 50 | + 'json' => $data, |
| 51 | + 'headers' => [ |
| 52 | + 'Content-Type' => 'application/json', |
| 53 | + ], |
| 54 | + ]), |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + public function updateUser(UserComApiAwareInterface $resource, int $userId, array $data): ?array |
| 59 | + { |
| 60 | + $url = $this->getApiEndpointUrl($resource, sprintf(self::UPDATE_USER_ENDPOINT, $userId)); |
| 61 | + |
| 62 | + return $this->request( |
| 63 | + $url, |
| 64 | + Request::METHOD_PUT, |
| 65 | + $this->buildOptions($resource, ['json' => $data]), |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + public function createUser(UserComApiAwareInterface $resource, array $data): ?array |
| 70 | + { |
| 71 | + return $this->request( |
| 72 | + $this->getApiEndpointUrl($resource, self::CREATE_USER_ENDPOINT), |
| 73 | + Request::METHOD_POST, |
| 74 | + $this->buildOptions($resource, [ |
| 75 | + 'json' => $data, |
| 76 | + 'headers' => [ |
| 77 | + 'Content-Type' => 'application/json', |
| 78 | + 'User-Agent' => 'BitBag SyliusUserComPlugin', |
| 79 | + ], |
| 80 | + ]), |
| 81 | + ); |
| 82 | + } |
| 83 | + |
| 84 | + public function mergeUsers(UserComApiAwareInterface $resource, int $parentId, array $usersList): ?array |
| 85 | + { |
| 86 | + return $this->request( |
| 87 | + $this->getApiEndpointUrl($resource, self::MERGE_USERS_ENDPOINT), |
| 88 | + Request::METHOD_POST, |
| 89 | + $this->buildOptions($resource, [ |
| 90 | + 'json' => [ |
| 91 | + self::PARENT => $parentId, |
| 92 | + self::USERS_LIST => $usersList, |
| 93 | + ], |
| 94 | + ]), |
| 95 | + ); |
| 96 | + } |
| 97 | +} |
0 commit comments