-
Notifications
You must be signed in to change notification settings - Fork 6
Feature/carlos turmo application test php #8
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,8 @@ | ||
| version: '2' | ||
| services: | ||
| php: | ||
| image: yiisoftware/yii2-php:7.4-apache | ||
| volumes: | ||
| - ~/.composer-docker/cache:/root/.composer/cache:delegated | ||
| - ./:/app:delegated | ||
| ports: | ||
| - '8000:80' | ||
| - '8080:80' |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| <?php | ||
|
|
||
| namespace app\models; | ||
|
|
||
| use yii\base\Model; | ||
|
|
||
| // Include Yii2 HTTP Client | ||
| use yii\httpclient\Client; | ||
|
|
||
| class Post extends Model | ||
| { | ||
| /* | ||
| Fetch all posts from the external JSONPlaceholder API with pagination support. | ||
| @param int $page The page number to fetch. | ||
| @param int $limit The number of posts per page. | ||
| @return array containing 'data' (posts) and 'totalCount' (total number of posts). | ||
| */ | ||
| public static function fetchAll($page = 1, $limit = 9) | ||
| { | ||
| // Create Yii2 HTTP Client | ||
| $client = new Client(['baseUrl' => 'https://jsonplaceholder.typicode.com/']); | ||
|
|
||
| // Try to send the request and handle possible errors | ||
| try { | ||
|
|
||
| // Send GET request to the /posts endpoint using Yii2 HttpClient with params for pagination | ||
| // Documentation: https://github.com/typicode/json-server#paginate | ||
| // We use _limit instead of _per_page because JSONPlaceholder is using old json-server version | ||
| $response = $client->get('posts', [ | ||
| '_page' => $page, | ||
| '_limit' => $limit, | ||
| ])->send(); | ||
|
|
||
| // Check if the response is successful | ||
| if ($response->isOk) { | ||
| $posts = $response->data; | ||
|
|
||
| // Total count of posts from the headers | ||
| $totalCount = (int) $response->headers->get('X-Total-Count', count($posts)); | ||
|
|
||
| // Return posts data and total count | ||
| return [ | ||
| 'data' => $posts, | ||
| 'totalCount' => $totalCount, | ||
| ]; | ||
| } | ||
|
|
||
| /* | ||
| Handle different error status codes from the API related with getting posts. | ||
| Taking into account it's not necessary credentials for this API. | ||
| */ | ||
| switch ($response->statusCode) { | ||
| case 400: | ||
| $message = 'Bad Request (400).'; | ||
| break; | ||
| case 404: | ||
| $message = 'Resource not found (404).'; | ||
| break; | ||
| case 500: | ||
| $message = 'Internal server error (500).'; | ||
| break; | ||
| case 503: | ||
| $message = 'Service unavailable (503). Please try again later.'; | ||
| break; | ||
| default: | ||
| $message = 'Unknown error. Code: ' . $response->statusCode; | ||
| break; | ||
| } | ||
|
|
||
| // Yii2 log to capture possible API errors | ||
| Yii::warning("Error API ({$response->statusCode}): {$message}", __METHOD__); | ||
|
|
||
| return [ | ||
| 'error' => true, | ||
| 'message' => $message, | ||
| ]; | ||
|
|
||
|
|
||
| } catch (\Exception $e) { | ||
|
|
||
| // Yii2 log to capture connection errors | ||
| Yii::error("Failed to connect: " . $e->getMessage(), __METHOD__); | ||
| return [ | ||
| 'error' => true, | ||
| 'message' => 'Sorry, there was an error connecting to the external {JSON} Placeholder API.', | ||
| ]; | ||
| } | ||
|
|
||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,9 @@ | |||||||
|
|
||||||||
| /** @var yii\web\View $this */ | ||||||||
|
|
||||||||
| /* Yii2 pagination widget */ | ||||||||
| use yii\widgets\LinkPager; | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's a namespace inconsistency with the LinkPager widget. At the top of the file, you import |
||||||||
|
|
||||||||
| $this->title = 'Test 1 — ooptimo'; | ||||||||
| ?> | ||||||||
| <div class="site-test1"> | ||||||||
|
|
@@ -25,9 +28,46 @@ | |||||||
|
|
||||||||
| <hr> | ||||||||
|
|
||||||||
| <div class="posts-list"> | ||||||||
| <div class="posts-list mb-5"> | ||||||||
| <div class="row mt-4"> | ||||||||
| <h2>Posts</h2> | ||||||||
| <h2 class="mb-4">Posts</h2> | ||||||||
| </div> | ||||||||
|
|
||||||||
| <div class="row"> | ||||||||
| <!-- Check if there was an error fetching posts --> | ||||||||
| <?php if ( isset($posts['error']) ): ?> | ||||||||
| <div class="col-md-12"> | ||||||||
| <div class="alert alert-danger" role="alert"> | ||||||||
| <!-- Display the error message --> | ||||||||
| <?php echo $posts['message']; ?> | ||||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error message is being output directly without HTML escaping, which could lead to XSS vulnerabilities if the error message contains HTML tags or scripts.
Suggested change
|
||||||||
| </div> | ||||||||
| </div> | ||||||||
| <?php else: ?> | ||||||||
| <!-- Loop and display each post --> | ||||||||
| <?php foreach ( $posts as $post ): ?> | ||||||||
| <div class="col-md-4 mb-4"> | ||||||||
| <div class="card h-100"> | ||||||||
| <div class="card-body"> | ||||||||
| <!-- Escape output to prevent HTML injection --> | ||||||||
| <h5 class="card-title"><?= htmlspecialchars($post['title']) ?></h5> | ||||||||
| <p class="card-text"><?= htmlspecialchars($post['body']) ?></p> | ||||||||
| </div> | ||||||||
| <div class="card-footer text-ooptimo"> | ||||||||
| <small>By <strong>User <?= htmlspecialchars($post['userId']) ?></strong> · Post <strong>#<?= htmlspecialchars($post['id']) ?></strong></small> | ||||||||
| </div> | ||||||||
| </div> | ||||||||
| </div> | ||||||||
| <?php endforeach; ?> | ||||||||
| <?php endif; ?> | ||||||||
| </div> | ||||||||
|
|
||||||||
| <div class="row"> | ||||||||
| <div class="col-md-12 align-center"> | ||||||||
| <div class="pagination-wrapper align-center mt-4"> | ||||||||
| <!-- Yii2 pagination widget, with Bootstrap 5 structure --> | ||||||||
| <?= \yii\bootstrap5\LinkPager::widget(['pagination' => $pagination]) ?> | ||||||||
| </div> | ||||||||
| </div> | ||||||||
| </div> | ||||||||
| </div> | ||||||||
| </div> | ||||||||
| </div> | ||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Yii class is being used for logging but hasn't been imported. Please add
use Yii;at the top of the file with the other imports.