Skip to content

Commit

Permalink
Added phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
daVitekPL authored Jul 24, 2024
2 parents 2c52b8b + 7cdab3d commit 02c1d65
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 8 deletions.
Binary file modified .DS_Store
Binary file not shown.
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 @@ -10,7 +10,8 @@
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"orchestra/testbench": "^6.0"
"orchestra/testbench": ">=7.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: 5
1 change: 1 addition & 0 deletions src/Export/UserGroupExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function __construct(Group $group)
public function collection(): Collection
{
return collect($this->usersArray)->map(function ($user) {
$result = [];
foreach ($this->userKeys as $key) {
$result[$key] = $user[$key] ?? '';
}
Expand Down
1 change: 1 addition & 0 deletions src/Export/UsersExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function __construct(Collection $users)
public function collection(): Collection
{
return collect($this->usersArray)->map(function ($user) {
$result = [];
foreach ($this->keys as $key) {
$result[$key] = $user[$key] ?? '';
}
Expand Down
2 changes: 2 additions & 0 deletions src/Import/UserGroupImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EscolaLms\CsvUsers\Import;

use EscolaLms\CsvUsers\Models\Group;
use EscolaLms\CsvUsers\Services\Contracts\CsvUserServiceContract;
use EscolaLms\CsvUsers\Services\CsvUserGroupService;
use Illuminate\Support\Collection;
Expand All @@ -24,6 +25,7 @@ public function collection(Collection $rows): void
$rows = $this->prepareDataToImport($rows);
$this->validateData($rows);

/** @var Group $group */
$group = $csvGroupService->saveGroupFromImport($this->prepareGroupData($rows->first()));

$users = collect();
Expand Down
4 changes: 4 additions & 0 deletions src/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use EscolaLms\Auth\Models\Group as AuthGroup;

/**
* @property string $name
* @property bool $registerable
*/
class Group extends AuthGroup
{
}
2 changes: 1 addition & 1 deletion src/Policies/CsvUserGroupsPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

namespace EscolaLms\CsvUsers\Policies;

use EscolaLms\Auth\Models\User;
use EscolaLms\Core\Enums\UserRole;
use EscolaLms\CsvUsers\Enums\CsvUserPermissionsEnum;
use Illuminate\Auth\Access\HandlesAuthorization;
use Illuminate\Foundation\Auth\User;

class CsvUserGroupsPolicy
{
Expand Down
2 changes: 1 addition & 1 deletion src/Policies/CsvUsersPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace EscolaLms\CsvUsers\Policies;

use Illuminate\Foundation\Auth\User;
use EscolaLms\Auth\Models\User;
use EscolaLms\Core\Enums\UserRole;
use EscolaLms\CsvUsers\Enums\CsvUserPermissionsEnum;
use Illuminate\Auth\Access\HandlesAuthorization;
Expand Down
8 changes: 6 additions & 2 deletions src/Services/CsvUserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

use EscolaLms\Auth\Dtos\UserFilterCriteriaDto;
use EscolaLms\Auth\Models\Group;
use EscolaLms\Auth\Models\User;
use EscolaLms\Auth\Repositories\Contracts\UserRepositoryContract;
use EscolaLms\CsvUsers\Events\EscolaLmsImportedNewUserTemplateEvent;
use EscolaLms\CsvUsers\Models\User;
use EscolaLms\CsvUsers\Services\Contracts\CsvUserServiceContract;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Collection;
Expand All @@ -32,10 +32,14 @@ public function saveUserFromImport(Collection $data, string $returnUrl): Model
$data->put('password', Hash::make($data->get('password')));
}

if ($user = $this->userRepository->findByEmail($data->get('email'))) {
/** @var User|null $user */
$user = $this->userRepository->findByEmail($data->get('email'));
if ($user) {
/** @var User $user */
$user = $this->userRepository->update($data->toArray(), $user->getKey());
} else {
$data->put('is_active', true);
/** @var User $user */
$user = $this->userRepository->create($data->toArray());
$user->markEmailAsVerified();
event(new EscolaLmsImportedNewUserTemplateEvent($user, $returnUrl));
Expand Down
6 changes: 3 additions & 3 deletions src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

Route::group(['middleware' => ['auth:api'], 'prefix' => 'api/admin/csv'], function () {
Route::prefix('users')->group(function () {
Route::get(null, [CsvUserAPIController::class, 'export']);
Route::post(null , [CsvUserAPIController::class, 'import']);
Route::get('', [CsvUserAPIController::class, 'export']);
Route::post('' , [CsvUserAPIController::class, 'import']);
});
Route::prefix('groups')->group(function () {
Route::get('{group}', [CsvGroupAPIController::class, 'export']);
Route::post(null , [CsvGroupAPIController::class, 'import']);
Route::post('' , [CsvGroupAPIController::class, 'import']);
});
});

0 comments on commit 02c1d65

Please sign in to comment.