From d211fa3d7db1ed0d8906f0586b82f50cfbb0a19e Mon Sep 17 00:00:00 2001 From: jurgenhaas <juergen.haas@lakedrops.com> Date: Sat, 29 Oct 2022 14:28:14 +0200 Subject: [PATCH] Issue #3310438 by jurgenhaas: Update dependencies --- composer.json | 50 +++++++------- src/Api.php | 24 ++++--- src/Entity/GitlabServer.php | 65 ++++++++++++++----- .../ListBuilder/GitlabServerListBuilder.php | 4 +- src/Event/CreateProject.php | 20 ++++-- src/Form/GitlabServerForm.php | 17 +++-- src/Form/Settings.php | 2 +- src/GitLabEvents.php | 2 +- src/Plugin/WebformHandler/CreateProject.php | 40 ++++-------- 9 files changed, 134 insertions(+), 90 deletions(-) diff --git a/composer.json b/composer.json index 386f73d..e8b3527 100644 --- a/composer.json +++ b/composer.json @@ -1,27 +1,29 @@ { - "name": "drupal/gitlab_api", - "type": "drupal-module", - "description": "Integrates your Drupal site into GitLab using the GitLab API.", - "keywords": [ - "Drupal", - "GitLab", - "API" - ], - "license": "GPL-2.0-or-later", - "homepage": "https://www.drupal.org/project/gitlab_api", - "authors": [ - { - "name": "Jürgen Haas", - "homepage": "https://www.drupal.org/u/jurgenhaas" - } - ], - "minimum-stability": "dev", - "support": { - "issues": "https://www.drupal.org/project/issues/gitlab_api", - "source": "https://git.drupal.org/project/gitlab_api" - }, - "require": { - "php": ">=7.4", - "m4tthumphrey/php-gitlab-api": "^11.8" + "name": "drupal/gitlab_api", + "type": "drupal-module", + "description": "Integrates your Drupal site into GitLab using the GitLab API.", + "keywords": [ + "Drupal", + "GitLab", + "API" + ], + "license": "GPL-2.0-or-later", + "homepage": "https://www.drupal.org/project/gitlab_api", + "authors": [ + { + "name": "Jürgen Haas", + "homepage": "https://www.drupal.org/u/jurgenhaas" } + ], + "minimum-stability": "dev", + "support": { + "issues": "https://www.drupal.org/project/issues/gitlab_api", + "source": "https://git.drupal.org/project/gitlab_api" + }, + "require": { + "php": ">=7.4", + "zeichen32/gitlabapibundle": "^6.0", + "symfony/http-client": "^5.4", + "nyholm/psr7": "^1.4" + } } diff --git a/src/Api.php b/src/Api.php index 9801ba8..7d77ad2 100644 --- a/src/Api.php +++ b/src/Api.php @@ -3,38 +3,44 @@ namespace Drupal\gitlab_api; use Drupal\Core\Config\ConfigFactoryInterface; +use Drupal\Core\Config\ImmutableConfig; use Drupal\gitlab_api\Entity\GitlabServer; use Gitlab\Client; -use Gitlab\Exception\InvalidArgumentException; use Gitlab\ResultPager; -use DateTime; /** - * Class Api + * GitLab API wrapper class. * * @package Drupal\gitlab_api */ class Api { /** + * The module config. + * * @var \Drupal\Core\Config\ImmutableConfig */ - protected $config; + protected ImmutableConfig $config; /** + * The GitLab client. + * * @var \Gitlab\Client */ - protected $client; + protected Client $client; /** - * @var GitlabServer + * The GitLab server entity. + * + * @var \Drupal\gitlab_api\Entity\GitlabServer */ - protected $server; + protected GitlabServer $server; /** * Api constructor. * * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory + * The Drupal config factory. */ public function __construct(ConfigFactoryInterface $config_factory) { $this->config = $config_factory->get('gitlab_api.settings'); @@ -43,11 +49,13 @@ class Api { /** * Initialize client and authenticate session. */ - protected function init() { + protected function init(): void { if (!isset($this->server)) { $this->switchServer(); } if (!isset($this->client)) { + $this->client = Client::createWithHttpClient(); + $this->client = Client::create($this->server->getUrl()); $this->client->authenticate($this->server->getAuthToken(), Client::AUTH_URL_TOKEN); } diff --git a/src/Entity/GitlabServer.php b/src/Entity/GitlabServer.php index 32f6e8d..f846c22 100644 --- a/src/Entity/GitlabServer.php +++ b/src/Entity/GitlabServer.php @@ -2,8 +2,9 @@ namespace Drupal\gitlab_api\Entity; +use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; +use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Core\Config\Entity\ConfigEntityBase; -use Drupal\Core\Config\Entity\ConfigEntityInterface; /** * Defines the gitlab server entity type. @@ -54,59 +55,89 @@ class GitlabServer extends ConfigEntityBase { * * @var string */ - protected $id; + protected string $id; /** * The gitlab server url. * * @var string */ - protected $url; + protected string $url; /** * The gitlab_server auth_token. * * @var string */ - protected $auth_token; + protected string $auth_token; /** - * The gitlab server status. + * Is it the default server. * * @var bool */ - protected $status; + protected bool $default_server; /** - * Is it the default server. + * Get the default GitLab server. * - * @var bool + * @return \Drupal\gitlab_api\Entity\GitlabServer|false + * The default GitLab server vondig entity or FALSE, if it doesn't exist. */ - protected $default_server; - public static function loadDefaultServer() { - $server = \Drupal::entityTypeManager()->getStorage('gitlab_server')->loadByProperties(['default_server' => TRUE]); + try { + $server = \Drupal::entityTypeManager() + ->getStorage('gitlab_server') + ->loadByProperties(['default_server' => TRUE]); + } + catch (InvalidPluginDefinitionException | PluginNotFoundException $e) { + return FALSE; + } return $server ? reset($server) : FALSE; } - public function getUrl() : string { + /** + * Get the server URL. + * + * @return string + * The server URL. + */ + public function getUrl(): string { return $this->get('url'); } - public function getAuthToken() : ?string { + /** + * Get the auth token. + * + * @return string|null + * The auth token or NULL if none is available. + */ + public function getAuthToken(): ?string { return $this->get('auth_token'); } - public function isDefault() : bool { + /** + * Determines if this server is the default one. + * + * @return bool + * TRUE, if this server is marked as default, FALSE otherwise. + */ + public function isDefault(): bool { if ($this->isNew() && !self::loadDefaultServer()) { return TRUE; } return (bool) $this->get('default_server'); } - public function setDefault(bool $isDefault = TRUE) { - $this->set('default_server', $isDefault); - return $this; + /** + * Make this server the default one. + * + * @param bool $isDefault + * Flag to tell, if this server should be default or not. + */ + public function setDefault(bool $isDefault = TRUE): GitlabServer { + $this->set('default_server', $isDefault); + return $this; } } diff --git a/src/Entity/ListBuilder/GitlabServerListBuilder.php b/src/Entity/ListBuilder/GitlabServerListBuilder.php index c03647c..163fd8e 100644 --- a/src/Entity/ListBuilder/GitlabServerListBuilder.php +++ b/src/Entity/ListBuilder/GitlabServerListBuilder.php @@ -13,7 +13,7 @@ class GitlabServerListBuilder extends ConfigEntityListBuilder { /** * {@inheritdoc} */ - public function buildHeader() { + public function buildHeader(): array { $header['label'] = $this->t('Server URL'); $header['id'] = $this->t('Machine name'); $header['status'] = $this->t('Status'); @@ -24,7 +24,7 @@ class GitlabServerListBuilder extends ConfigEntityListBuilder { /** * {@inheritdoc} */ - public function buildRow(EntityInterface $entity) { + public function buildRow(EntityInterface $entity): array { /** @var \Drupal\gitlab_api\Entity\GitlabServer $entity */ $row['label'] = $entity->label(); $row['id'] = $entity->id(); diff --git a/src/Event/CreateProject.php b/src/Event/CreateProject.php index d3fde83..accdd88 100644 --- a/src/Event/CreateProject.php +++ b/src/Event/CreateProject.php @@ -2,31 +2,37 @@ namespace Drupal\gitlab_api\Event; +use Drupal\Component\EventDispatcher\Event; use Drupal\webform\WebformSubmissionInterface; -use Symfony\Component\EventDispatcher\Event; /** - * Class CreateProject Event + * Dispatches after the Webform handler created a new project. * * @package Drupal\gitlab_api\Event */ class CreateProject extends Event { /** + * The webform submission entity. + * * @var \Drupal\webform\WebformSubmissionInterface */ - protected $webformSubmission; + protected WebformSubmissionInterface $webformSubmission; /** + * The created project from the GitLab API. + * * @var array */ - protected $project; + protected array $project; /** * CreateProject Event constructor. * * @param \Drupal\webform\WebformSubmissionInterface $webform_submission + * The webform submission entity. * @param array $project + * The created project from the GitLab API. */ public function __construct(WebformSubmissionInterface $webform_submission, array $project) { $this->webformSubmission = $webform_submission; @@ -34,14 +40,20 @@ class CreateProject extends Event { } /** + * Gets the webform submission entity. + * * @return \Drupal\webform\WebformSubmissionInterface + * The webform submission entity. */ public function getWebformSubmission(): WebformSubmissionInterface { return $this->webformSubmission; } /** + * Gets the created project from the GitLab API. + * * @return array + * The created project from the GitLab API. */ public function getProject(): array { return $this->project; diff --git a/src/Form/GitlabServerForm.php b/src/Form/GitlabServerForm.php index af3775d..4acb300 100644 --- a/src/Form/GitlabServerForm.php +++ b/src/Form/GitlabServerForm.php @@ -16,7 +16,7 @@ class GitlabServerForm extends EntityForm { /** * {@inheritdoc} */ - public function form(array $form, FormStateInterface $form_state) { + public function form(array $form, FormStateInterface $form_state): array { $server = $this->entity; $form = parent::form($form, $form_state); @@ -58,15 +58,17 @@ class GitlabServerForm extends EntityForm { $form['status'] = [ '#type' => 'checkbox', '#title' => $this->t('Enabled'), - '#default_value' => $server->isNew() ? TRUE : $server->status(), + '#default_value' => $server->isNew() || $server->status(), ]; return $form; } - public function validateForm(array &$form, FormStateInterface $form_state) { + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, FormStateInterface $form_state): void { parent::validateForm($form, $form_state); - /** @var \Drupal\gitlab_api\Entity\GitlabServer $server */ $server = $this->entity; if (!$form_state->getValue('default_server')) { @@ -83,8 +85,11 @@ class GitlabServerForm extends EntityForm { /** * {@inheritdoc} + * + * @throws \Drupal\Core\Entity\EntityStorageException + * @throws \Drupal\Core\Entity\EntityMalformedException */ - public function save(array $form, FormStateInterface $form_state) { + public function save(array $form, FormStateInterface $form_state): int { if ($this->entity->isDefault() && $defaultServer = GitlabServer::loadDefaultServer()) { $defaultServer->setDefault(FALSE); @@ -93,7 +98,7 @@ class GitlabServerForm extends EntityForm { $result = parent::save($form, $form_state); $message_args = ['%label' => $this->entity->label()]; - $message = $result == SAVED_NEW + $message = $result === SAVED_NEW ? $this->t('Created new gitlab server %label.', $message_args) : $this->t('Updated gitlab server %label.', $message_args); $this->messenger()->addStatus($message); diff --git a/src/Form/Settings.php b/src/Form/Settings.php index 2a231b6..299be3a 100644 --- a/src/Form/Settings.php +++ b/src/Form/Settings.php @@ -47,7 +47,7 @@ class Settings extends ConfigFormBase { /** * {@inheritdoc} */ - public function submitForm(array &$form, FormStateInterface $form_state) { + public function submitForm(array &$form, FormStateInterface $form_state): void { /** @var \Drupal\Core\Config\Config $config */ $config = $this->config('gitlab_api.settings'); $config diff --git a/src/GitLabEvents.php b/src/GitLabEvents.php index 3de1268..de7cdca 100644 --- a/src/GitLabEvents.php +++ b/src/GitLabEvents.php @@ -7,6 +7,6 @@ namespace Drupal\gitlab_api; */ final class GitLabEvents { - const CreateProject = 'create project'; + public const CREATEPROJECT = 'create project'; } diff --git a/src/Plugin/WebformHandler/CreateProject.php b/src/Plugin/WebformHandler/CreateProject.php index c79fbb3..a7dd6d4 100644 --- a/src/Plugin/WebformHandler/CreateProject.php +++ b/src/Plugin/WebformHandler/CreateProject.php @@ -2,15 +2,11 @@ namespace Drupal\gitlab_api\Plugin\WebformHandler; -use Drupal\Core\Config\ConfigFactoryInterface; -use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; -use Drupal\Core\Logger\LoggerChannelFactoryInterface; use Drupal\gitlab_api\Api; use Drupal\gitlab_api\Event\CreateProject as CreateProjectEvent; use Drupal\gitlab_api\GitLabEvents; use Drupal\webform\Plugin\WebformHandlerBase; -use Drupal\webform\WebformSubmissionConditionsValidatorInterface; use Drupal\webform\WebformSubmissionInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -32,30 +28,24 @@ use Symfony\Component\EventDispatcher\EventDispatcherInterface; class CreateProject extends WebformHandlerBase { /** + * The GitLab API service. + * * @var \Drupal\gitlab_api\Api */ - protected $api; + protected Api $api; /** + * The event dispatcher. + * * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface */ - protected $eventDispatcher; + protected EventDispatcherInterface $eventDispatcher; /** - * CreateProject constructor. - * - * @param array $configuration - * @param $plugin_id - * @param $plugin_definition - * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory - * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory - * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager - * @param \Drupal\webform\WebformSubmissionConditionsValidatorInterface $conditions_validator - * @param \Drupal\gitlab_api\Api $api - * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $event_dispatcher + * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, LoggerChannelFactoryInterface $logger_factory, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager, WebformSubmissionConditionsValidatorInterface $conditions_validator, Api $api, EventDispatcherInterface $event_dispatcher) { - parent::__construct($configuration, $plugin_id, $plugin_definition, $logger_factory, $config_factory, $entity_type_manager, $conditions_validator); + public function __construct(array $configuration, $plugin_id, $plugin_definition, Api $api, EventDispatcherInterface $event_dispatcher) { + parent::__construct($configuration, $plugin_id, $plugin_definition); $this->api = $api; $this->eventDispatcher = $event_dispatcher; } @@ -63,15 +53,11 @@ class CreateProject extends WebformHandlerBase { /** * {@inheritdoc} */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): CreateProject { return new static( $configuration, $plugin_id, $plugin_definition, - $container->get('logger.factory'), - $container->get('config.factory'), - $container->get('entity_type.manager'), - $container->get('webform_submission.conditions_validator'), $container->get('gitlab_api.api'), $container->get('event_dispatcher') ); @@ -141,7 +127,7 @@ class CreateProject extends WebformHandlerBase { /** * {@inheritdoc} */ - public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + public function submitConfigurationForm(array &$form, FormStateInterface $form_state): void { parent::submitConfigurationForm($form, $form_state); $this->applyFormStateToConfiguration($form_state); } @@ -149,7 +135,7 @@ class CreateProject extends WebformHandlerBase { /** * {@inheritdoc} */ - public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE) { + public function postSave(WebformSubmissionInterface $webform_submission, $update = TRUE): void { if (!$update && $webform_submission->getState() === WebformSubmissionInterface::STATE_COMPLETED) { $data = $webform_submission->getData(); @@ -162,7 +148,7 @@ class CreateProject extends WebformHandlerBase { $webform_submission->setData($data); $webform_submission->resave(); $event = new CreateProjectEvent($webform_submission, $project); - $this->eventDispatcher->dispatch(GitLabEvents::CreateProject, $event); + $this->eventDispatcher->dispatch($event, GitLabEvents::CREATEPROJECT); } } -- GitLab