Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EZP-30885: Introduced strict types for LanguageService #2811

Merged
merged 1 commit into from
Oct 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 28 additions & 28 deletions eZ/Publish/API/Repository/LanguageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,77 +19,77 @@ interface LanguageService
/**
* Creates the a new Language in the content repository.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the languageCode already exists
*
* @param \eZ\Publish\API\Repository\Values\Content\LanguageCreateStruct $languageCreateStruct
*
* @return \eZ\Publish\API\Repository\Values\Content\Language
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the languageCode already exists
*/
public function createLanguage(LanguageCreateStruct $languageCreateStruct);
public function createLanguage(LanguageCreateStruct $languageCreateStruct): Language;

/**
* Changes the name of the language in the content repository.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
*
* @param \eZ\Publish\API\Repository\Values\Content\Language $language
* @param string $newName
*
* @return \eZ\Publish\API\Repository\Values\Content\Language
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
*/
public function updateLanguageName(Language $language, $newName);
public function updateLanguageName(Language $language, string $newName): Language;

/**
* Enables a language.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
*
* @param \eZ\Publish\API\Repository\Values\Content\Language $language
*
* @return \eZ\Publish\API\Repository\Values\Content\Language
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
*/
public function enableLanguage(Language $language);
public function enableLanguage(Language $language): Language;

/**
* Disables a language.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
*
* @param \eZ\Publish\API\Repository\Values\Content\Language $language
*
* @return \eZ\Publish\API\Repository\Values\Content\Language
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
*/
public function disableLanguage(Language $language);
public function disableLanguage(Language $language): Language;

/**
* Loads a Language from its language code ($languageCode).
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if language could not be found
*
* @param string $languageCode
*
* @return \eZ\Publish\API\Repository\Values\Content\Language
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if language could not be found
*/
public function loadLanguage($languageCode);
public function loadLanguage(string $languageCode): Language;

/**
* Loads all Languages.
*
* @return \eZ\Publish\API\Repository\Values\Content\Language[]
*/
public function loadLanguages();
public function loadLanguages(): iterable;

/**
* Loads a Language by its id ($languageId).
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if language could not be found
*
* @param mixed $languageId
* @param int $languageId
*
* @return \eZ\Publish\API\Repository\Values\Content\Language
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if language could not be found
*/
public function loadLanguageById($languageId);
public function loadLanguageById(int $languageId): Language;

/**
* Bulk-load Languages by language codes.
Expand All @@ -116,26 +116,26 @@ public function loadLanguageListById(array $languageIds): iterable;
/**
* Deletes a language from content repository.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* if language can not be deleted
* @param \eZ\Publish\API\Repository\Values\Content\Language $language
*
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if language can not be deleted
* because it is still assigned to some content / type / (...).
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user is not allowed to delete a language
*
* @param \eZ\Publish\API\Repository\Values\Content\Language $language
*/
public function deleteLanguage(Language $language);
public function deleteLanguage(Language $language): void;

/**
* Returns a configured default language code.
*
* @return string
*/
public function getDefaultLanguageCode();
public function getDefaultLanguageCode(): string;

/**
* Instantiates an object to be used for creating languages.
*
* @return \eZ\Publish\API\Repository\Values\Content\LanguageCreateStruct
*/
public function newLanguageCreateStruct();
public function newLanguageCreateStruct(): LanguageCreateStruct;
}
11 changes: 6 additions & 5 deletions eZ/Publish/API/Repository/Tests/LanguageServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/
namespace eZ\Publish\API\Repository\Tests;

use eZ\Publish\API\Repository\Exceptions\InvalidArgumentException;
use eZ\Publish\API\Repository\Exceptions\NotFoundException;
use Exception;
use eZ\Publish\API\Repository\Values\Content\Language;
Expand Down Expand Up @@ -128,7 +129,7 @@ public function testCreateLanguageSetsExpectedProperties($language)
*/
public function testCreateLanguageThrowsInvalidArgumentException()
{
$this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Argument \'languageCreateStruct\' is invalid: language with specified language code already exists');

$repository = $this->getRepository();
Expand Down Expand Up @@ -263,7 +264,7 @@ public function testUpdateLanguageName()
*/
public function testUpdateLanguageNameThrowsInvalidArgumentException()
{
$this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Argument \'newName\' is invalid: \'\' is wrong value');

$repository = $this->getRepository();
Expand Down Expand Up @@ -412,12 +413,12 @@ public function testLoadLanguageThrowsNotFoundException()
*/
public function testLoadLanguageThrowsInvalidArgumentException()
{
$this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Argument \'languageCode\' is invalid: language code has an invalid value');

$repository = $this->getRepository();

$repository->getContentLanguageService()->loadLanguage(PHP_INT_MAX);
$repository->getContentLanguageService()->loadLanguage('');
}

/**
Expand Down Expand Up @@ -531,7 +532,7 @@ public function testDeleteLanguage()
*/
public function testDeleteLanguageThrowsInvalidArgumentException()
{
$this->expectException(\eZ\Publish\API\Repository\Exceptions\InvalidArgumentException::class);
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Argument \'language\' is invalid: Deleting language logic error, some content still references that language and therefore it can\'t be deleted');

$repository = $this->getRepository();
Expand Down
2 changes: 1 addition & 1 deletion eZ/Publish/Core/Event/LanguageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function createLanguage(LanguageCreateStruct $languageCreateStruct): Lang

public function updateLanguageName(
Language $language,
$newName
string $newName
): Language {
$eventData = [
$language,
Expand Down
60 changes: 27 additions & 33 deletions eZ/Publish/Core/Repository/LanguageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ public function __construct(
/**
* Creates the a new Language in the content repository.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the languageCode already exists
*
* @param \eZ\Publish\API\Repository\Values\Content\LanguageCreateStruct $languageCreateStruct
*
* @return \eZ\Publish\API\Repository\Values\Content\Language
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the languageCode already exists
*/
public function createLanguage(LanguageCreateStruct $languageCreateStruct)
public function createLanguage(LanguageCreateStruct $languageCreateStruct): Language
{
if (!is_string($languageCreateStruct->name) || empty($languageCreateStruct->name)) {
throw new InvalidArgumentValue('name', $languageCreateStruct->name, 'LanguageCreateStruct');
Expand Down Expand Up @@ -121,18 +121,17 @@ public function createLanguage(LanguageCreateStruct $languageCreateStruct)
/**
* Changes the name of the language in the content repository.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument
* is not string
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
*
* @param \eZ\Publish\API\Repository\Values\Content\Language $language
* @param string $newName
*
* @return \eZ\Publish\API\Repository\Values\Content\Language
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument
* is not string
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
*/
public function updateLanguageName(Language $language, $newName)
public function updateLanguageName(Language $language, string $newName): Language
{
if (!is_string($newName) || empty($newName)) {
if (empty($newName)) {
throw new InvalidArgumentValue('newName', $newName);
}

Expand Down Expand Up @@ -166,13 +165,12 @@ public function updateLanguageName(Language $language, $newName)
/**
* Enables a language.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
*
* @param \eZ\Publish\API\Repository\Values\Content\Language $language
*
* @return \eZ\Publish\API\Repository\Values\Content\Language
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
*/
public function enableLanguage(Language $language)
public function enableLanguage(Language $language): Language
{
if (!$this->permissionResolver->canUser('content', 'translations', $language)) {
throw new UnauthorizedException('content', 'translations');
Expand Down Expand Up @@ -204,13 +202,12 @@ public function enableLanguage(Language $language)
/**
* Disables a language.
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
*
* @param \eZ\Publish\API\Repository\Values\Content\Language $language
*
* @return \eZ\Publish\API\Repository\Values\Content\Language
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
*/
public function disableLanguage(Language $language)
public function disableLanguage(Language $language): Language
{
if (!$this->permissionResolver->canUser('content', 'translations', $language)) {
throw new UnauthorizedException('content', 'translations');
Expand Down Expand Up @@ -242,17 +239,16 @@ public function disableLanguage(Language $language)
/**
* Loads a Language from its language code ($languageCode).
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument
* is not string
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if language could not be found
*
* @param string $languageCode
*
* @return \eZ\Publish\API\Repository\Values\Content\Language
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if languageCode argument
* is not string
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if language could not be found
*/
public function loadLanguage($languageCode)
public function loadLanguage(string $languageCode): Language
{
if (!is_string($languageCode) || empty($languageCode)) {
if (empty($languageCode)) {
throw new InvalidArgumentException('languageCode', 'language code has an invalid value');
}

Expand All @@ -266,7 +262,7 @@ public function loadLanguage($languageCode)
*
* @return \eZ\Publish\API\Repository\Values\Content\Language[]
*/
public function loadLanguages()
public function loadLanguages(): iterable
{
$languages = $this->languageHandler->loadAll();

Expand All @@ -281,13 +277,12 @@ public function loadLanguages()
/**
* Loads a Language by its id ($languageId).
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if language could not be found
*
* @param mixed $languageId
*
* @return \eZ\Publish\API\Repository\Values\Content\Language
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if language could not be found
*/
public function loadLanguageById($languageId)
public function loadLanguageById(int $languageId): Language
{
$language = $this->languageHandler->load($languageId);

Expand Down Expand Up @@ -327,14 +322,13 @@ public function loadLanguageListById(array $languageIds): iterable
/**
* Deletes a language from content repository.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
* if language can not be deleted
* @param \eZ\Publish\API\Repository\Values\Content\Language $language
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if language can not be deleted
* because it is still assigned to some content / type / (...).
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException If user does not have access to content translations
*
* @param \eZ\Publish\API\Repository\Values\Content\Language $language
*/
public function deleteLanguage(Language $language)
public function deleteLanguage(Language $language): void
{
if (!$this->permissionResolver->canUser('content', 'translations', $language)) {
throw new UnauthorizedException('content', 'translations');
Expand All @@ -360,7 +354,7 @@ public function deleteLanguage(Language $language)
*
* @return string
*/
public function getDefaultLanguageCode()
public function getDefaultLanguageCode(): string
{
return $this->settings['languages'][0];
}
Expand All @@ -381,7 +375,7 @@ public function getPrioritizedLanguageCodeList()
*
* @return \eZ\Publish\API\Repository\Values\Content\LanguageCreateStruct
*/
public function newLanguageCreateStruct()
public function newLanguageCreateStruct(): LanguageCreateStruct
{
return new LanguageCreateStruct();
}
Expand Down
Loading