Skip to content

Commit

Permalink
Added phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
daVitekPL committed Jul 19, 2024
1 parent a94d9ee commit d084c63
Show file tree
Hide file tree
Showing 8 changed files with 105 additions and 18 deletions.
39 changes: 39 additions & 0 deletions .github/workflows/phpstan.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Tests PHPStan in environments

on: [pull_request]

jobs:
php82-laravel-latest-phpstan-postgres:
runs-on: ubuntu-latest
container:
image: escolalms/php:8.2

services:
postgres:
image: postgres:12
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: test
TZ: Europe/Warsaw
ports:
- 5432:5432

steps:
- name: Instantiate package
uses: actions/checkout@v2

- name: Update composer
run: COMPOSER_ROOT_VERSION=0.9.9 composer update

- name: Setup environment
run: cp env/postgres/* .

- name: Clear config
run: vendor/bin/testbench config:clear

- name: Publish things
run: vendor/bin/testbench migrate:fresh

- name: Run tests
run: vendor/bin/phpstan analyse
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"orchestra/testbench": ">=7.0",
"escolalms/courses": "^0",
"escolalms/course-access": "^0",
"escolalms/webinar": "^0"
"escolalms/webinar": "^0",
"nunomaduro/larastan": "^2.0"
},
"license": "MIT",
"authors": [
Expand Down
10 changes: 10 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
includes:
- ./vendor/nunomaduro/larastan/extension.neon

parameters:

paths:
- src/

# The level 9 is the highest level
level: 9
3 changes: 2 additions & 1 deletion src/EscolaLmsMattermostServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@

class EscolaLmsMattermostServiceProvider extends ServiceProvider
{
public $singletons = [
/** @var array<class-string, class-string> */
public array $singletons = [
MattermostServiceContract::class => MattermostService::class,
];

Expand Down
13 changes: 10 additions & 3 deletions src/Http/Controllers/MattermostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace EscolaLms\Mattermost\Http\Controllers;

use EscolaLms\Core\Http\Controllers\EscolaLmsBaseController;
use EscolaLms\Core\Models\User;
use EscolaLms\Mattermost\Http\Controllers\Swagger\MattermostSwagger;
use EscolaLms\Mattermost\Services\Contracts\MattermostServiceContract;
use Illuminate\Http\JsonResponse;
Expand All @@ -20,16 +21,22 @@ public function __construct(MattermostServiceContract $service)

public function me(Request $request): JsonResponse
{
return $this->sendResponse($this->service->getUserData(Auth::user()));
/** @var User $user */
$user = Auth::user();
return $this->sendResponse($this->service->getUserData($user));
}

public function generateCredentials(Request $request): JsonResponse
{
return $this->sendResponse($this->service->generateUserCredentials(Auth::user()));
/** @var User $user */
$user = Auth::user();
return $this->sendResponse($this->service->generateUserCredentials($user));
}

public function resetPassword(Request $request): JsonResponse
{
return $this->sendResponse($this->service->sendUserResetPassword(Auth::user()));
/** @var User $user */
$user = Auth::user();
return $this->sendResponse($this->service->sendUserResetPassword($user));
}
}
2 changes: 1 addition & 1 deletion src/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

class EventServiceProvider extends ServiceProvider
{
public function boot()
public function boot(): void
{
if (Config::get(SettingsServiceProvider::CONFIG_KEY . '.package_status', PackageStatusEnum::ENABLED) !== PackageStatusEnum::ENABLED) {
return;
Expand Down
16 changes: 11 additions & 5 deletions src/Services/Contracts/MattermostServiceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,33 @@ interface MattermostServiceContract
{
public function addUser(User $user): bool;

public function addUserToTeam(User $user, $teamDisplayName = "Courses"): bool;
public function addUserToTeam(User $user, string $teamDisplayName = "Courses"): bool;

public function addUserToChannel(User $user, $channelDisplayName, $teamDisplayName = "Courses", $channelRole = MattermostRoleEnum::MEMBER): bool;
public function addUserToChannel(User $user, string $channelDisplayName, string $teamDisplayName = "Courses", string $channelRole = MattermostRoleEnum::MEMBER): bool;

public function getOrCreateTeam(string $displayName): ResponseInterface;

public function getOrCreateChannel(string $teamDisplayName, string $channelDisplayName): ResponseInterface;

public function getOrCreateUser(User $user): ResponseInterface;

public function sendMessage(string $markdown, $channelDisplayName, $teamDisplayName = "Courses"): bool;
public function sendMessage(string $markdown, string $channelDisplayName, string $teamDisplayName = "Courses"): bool;

/**
* @return array<string, mixed>
*/
public function getUserData(User $user): array;

/**
* @return array<string, mixed>
*/
public function generateUserCredentials(User $user): array;

public function sendUserResetPassword($user): bool;
public function sendUserResetPassword(User $user): bool;

public function blockUser(User $user): bool;

public function deleteUser(User $user): bool;

public function removeUserFromChannel(User $user, $channelDisplayName, $teamDisplayName = "Courses"): bool;
public function removeUserFromChannel(User $user, string $channelDisplayName, string $teamDisplayName = "Courses"): bool;
}
37 changes: 30 additions & 7 deletions src/Services/MattermostService.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ private function getUsername(User $user): string
return Str::slug($user->email);
}

private function getData(ResponseInterface $result)
private function getData(ResponseInterface $result): mixed
{
return json_decode($result->getBody());
}

// @phpstan-ignore-next-line
private function logResponse(ResponseInterface $result): void
{
if ($result->getStatusCode() < 400) {
Expand All @@ -48,11 +49,12 @@ public function addUser(User $user): bool
return $result->getStatusCode() < 400;
}

public function addUserToTeam(User $user, $teamDisplayName = TeamNameEnum::COURSES): bool
public function addUserToTeam(User $user, string $teamDisplayName = TeamNameEnum::COURSES): bool
{
$team = $this->getData($this->getOrCreateTeam($teamDisplayName));
$user = $this->getData($this->getOrCreateUser($user));

// @phpstan-ignore-next-line
if (isset($team->id) && isset($user->id)) {
$teams = $this->driver->getTeamModel();
$result = $teams->addUser($team->id, [
Expand All @@ -66,12 +68,13 @@ public function addUserToTeam(User $user, $teamDisplayName = TeamNameEnum::COURS
return false;
}

public function addUserToChannel(User $user, $channelDisplayName, $teamDisplayName = TeamNameEnum::COURSES,
$channelRole = MattermostRoleEnum::MEMBER): bool
public function addUserToChannel(User $user, string $channelDisplayName, string $teamDisplayName = TeamNameEnum::COURSES,
string $channelRole = MattermostRoleEnum::MEMBER): bool
{
$channel = $this->getData($this->getOrCreateChannel($teamDisplayName, $channelDisplayName));
$mmUser = $this->getData($this->getOrCreateUser($user));

// @phpstan-ignore-next-line
if (isset($channel->id) && isset($mmUser->id)) {
$this->addUserToTeam($user, $teamDisplayName);
$channels = $this->driver->getChannelModel();
Expand Down Expand Up @@ -117,6 +120,7 @@ public function getOrCreateChannel(string $teamDisplayName, string $channelDispl

$channelName = Str::slug($channelDisplayName);

// @phpstan-ignore-next-line
if (isset($team->id)) {
$channels = $this->driver->getChannelModel();
$result = $channels->getChannelByName($team->id, $channelName);
Expand All @@ -135,6 +139,7 @@ public function getOrCreateChannel(string $teamDisplayName, string $channelDispl
return $result;
}

// @phpstan-ignore-next-line
return $team;
}

Expand All @@ -161,16 +166,18 @@ public function getOrCreateUser(User $user): ResponseInterface
return $result;
}

public function sendMessage(string $markdown, $channelDisplayName, $teamDisplayName = TeamNameEnum::COURSES): bool
public function sendMessage(string $markdown, string $channelDisplayName, string $teamDisplayName = TeamNameEnum::COURSES): bool
{
$channels = $this->driver->getChannelModel();

$channel = $channels->getChannelByNameAndTeamName(Str::slug($teamDisplayName), Str::slug($channelDisplayName));

$channelData = $this->getData($channel);

// @phpstan-ignore-next-line
if ($channelData->id) {
$result = $this->driver->getPostModel()->createPost([
// @phpstan-ignore-next-line
'channel_id' => $channelData->id,
'message' => $markdown,
]);
Expand All @@ -181,6 +188,9 @@ public function sendMessage(string $markdown, $channelDisplayName, $teamDisplayN
return false;
}

/**
* @return array<string, mixed>
*/
public function generateUserCredentials(User $user): array
{
$mmUser = json_decode($this->getOrCreateUser($user)->getBody());
Expand All @@ -189,6 +199,7 @@ public function generateUserCredentials(User $user): array

$newPassword = Str::random() . rand(0, 9) . '!';

// @phpstan-ignore-next-line
$result = $users->updateUserPassword($mmUser->id, [
'new_password' => $newPassword,
]);
Expand All @@ -202,6 +213,9 @@ public function generateUserCredentials(User $user): array
];
}

/**
* @return array<string, mixed>
*/
public function getUserData(User $user): array
{
$server = config('mattermost.servers.default.host');
Expand All @@ -218,18 +232,24 @@ public function getUserData(User $user): array

$teams = $this->driver->getTeamModel();

// @phpstan-ignore-next-line
$result = $teams->getUserTeams($userData->id);

$userTeamsData = json_decode($result->getBody());

$channels = $this->driver->getChannelModel();

// @phpstan-ignore-next-line
foreach ($userTeamsData as $userTeamData) {
// @phpstan-ignore-next-line
$result = $channels->getChannelsForUser($userData->id, $userTeamData->id);
$channelsData = json_decode($result->getBody());
// @phpstan-ignore-next-line
foreach ($channelsData as $channelData) {
// @phpstan-ignore-next-line
$channelData->url = 'https://' . $server . '/' . $userTeamData->name . '/' . $channelData->name;
}
// @phpstan-ignore-next-line
$userTeamData->channels = $channelsData;
}

Expand All @@ -239,7 +259,7 @@ public function getUserData(User $user): array
];
}

public function sendUserResetPassword($user): bool
public function sendUserResetPassword(User $user): bool
{
$this->getOrCreateUser($user);

Expand All @@ -257,6 +277,7 @@ public function blockUser(User $user): bool

if ($result->getStatusCode() === 200) {
$user = $this->getData($result);
// @phpstan-ignore-next-line
$result = $userModel->updateUserActive($user->id, ['active' => false]);
}

Expand All @@ -270,20 +291,22 @@ public function deleteUser(User $user): bool

if ($result->getStatusCode() === 200) {
$user = $this->getData($result);
// @phpstan-ignore-next-line
$result = $userModel->deactivateUserAccount($user->id);
}

return $result->getStatusCode() === 200;
}

public function removeUserFromChannel(User $user, $channelDisplayName, $teamDisplayName = TeamNameEnum::COURSES): bool
public function removeUserFromChannel(User $user, string $channelDisplayName, string $teamDisplayName = TeamNameEnum::COURSES): bool
{
$channelModel = $this->driver->getChannelModel();
$mmUser = $this->getData($this->driver->getUserModel()->getUserByEmail($user->email));
$channel = $this->getData(
$channelModel->getChannelByNameAndTeamName(Str::slug($teamDisplayName), Str::slug($channelDisplayName))
);

// @phpstan-ignore-next-line
if (isset($mmUser->id) && isset($channel->id)) {
$response = $channelModel->removeUserFromChannel($channel->id, $mmUser->id);

Expand Down

0 comments on commit d084c63

Please sign in to comment.