Giter Site home page Giter Site logo

laravel-bitly's Introduction

Laravel Bitly Package

A laravel package for generating Bitly short URLs.

For more information see Bitly

Build Status Latest Stable Version License Total Downloads

Requirements

Laravel 5.1 or later

Installation

Installation is a quick 3 step process:

  1. Download laravel-bitly using composer
  2. Enable the package in app.php
  3. Configure your Bitly credentials
  4. (Optional) Configure the package facade

Step 1: Download laravel-bitly using composer

Add shivella/laravel-bitly by running the command:

composer require shivella/laravel-bitly

Step 2: Enable the package in app.php

Register the Service in: config/app.php

Shivella\Bitly\BitlyServiceProvider::class,

Step 3: Configure Bitly credentials

php artisan vendor:publish --provider="Shivella\Bitly\BitlyServiceProvider"

Add this in you .env file

BITLY_ACCESS_TOKEN=your_secret_bitly_access_token

Step 4 (Optional): Configure the package facade

Register the Bitly Facade in: config/app.php

<?php

return [
    'aliases' => [

        'App' => Illuminate\Support\Facades\App::class,
        'Artisan' => Illuminate\Support\Facades\Artisan::class,
        'Auth' => Illuminate\Support\Facades\Auth::class,
        // ...
        'Bitly' => Shivella\Bitly\Facade\Bitly::class,
    ],
    // ...
];

Usage

<?php

$url = app('bitly')->getUrl('https://www.google.com/'); // http://bit.ly/nHcn3

Or if you want to use facade, add this in your class after namespace declaration:

<?php

use Bitly;

Then you can use it directly by calling Bitly:: like:

<?php

$url = Bitly::getUrl('https://www.google.com/'); // http://bit.ly/nHcn3

Testing

In your unit tests you may use BitlyClientFake class instead of regular client. It will create a fake short URLs using hashing without calling an external REST API, which will speed up your unit tests. Fake might be setup via DI at your \Tests\TestCase::createApplication() implementation:

<?php

namespace Tests;

use Illuminate\Contracts\Console\Kernel;
use Shivella\Bitly\Testing\BitlyClientFake;

trait CreatesApplication
{
    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make(Kernel::class)->bootstrap();

        // swap Bitly client by a fake
        $app->singleton('bitly', function () {
            return new BitlyClientFake();
        });

        return $app;
    }
}

As an alternative you may use \Shivella\Bitly\Facade\Bitly::fake() method to swap regular client by a fake.

laravel-bitly's People

Contributors

adrianenriquez avatar bomshteyn avatar klimov-paul avatar laravel-shift avatar lucidlogic avatar ryusuke005 avatar shivella avatar sweebee avatar taruncx101 avatar trevorgehman avatar viniciusls avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

laravel-bitly's Issues

Guzzle 7.1.0

Hi, will this package support guzzlehttp/guzzle 7.1.0?

Analytics or Clicks

Hello Shivella,

I want to commend you for such a great package. It works great! I wanted to find out if there is a way to track clicks or get the analytics from the bitly API?

Also, one thing I noticed was that when a URL has already been processed (shortened) and for some reason, you fire the shortened event again on the same URL, it returns the below:

message: "Client error: POST https://api-ssl.bitly.com/v4/shorten resulted in a 400 Bad Request response:↵{"message":"ALREADY_A_BITLY_LINK","resource":"bitlinks","description":"The value provided is invalid.","errors":[{"field (truncated...)↵"

Thank you and hoping to hear from you soon.

Can't install with Laravel 9

sail composer require shivella/laravel-bitly throws:

Problem 1
    - shivella/laravel-bitly[1.1.0, ..., 1.1.1] require illuminate/support ^5.1 -> found illuminate/support[v5.1.1, ..., 5.8.x-dev] but these were not loaded, likely because it conflicts with another require.
    - shivella/laravel-bitly 1.1.2 requires illuminate/support ^5.1 || ^6.0 -> found illuminate/support[v5.1.1, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev] but these were not loaded, likely because it conflicts with another require.
    - shivella/laravel-bitly[1.1.3, ..., 1.1.5] require illuminate/support ^5.8 || ^6.0 -> found illuminate/support[v5.8.0, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev] but these were not loaded, likely because it conflicts with another require.
    - shivella/laravel-bitly[1.1.6, ..., 1.1.7] require illuminate/support ^5.8 || ^6.0 || ^7.0 -> found illuminate/support[v5.8.0, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev, v7.0.0, ..., 7.x-dev] but these were not loaded, likely because it conflicts with another require.
    - shivella/laravel-bitly[1.1.8, ..., 1.1.11] require illuminate/support ^5.8 || ^6.0 || ^7.0 || ^8.0 -> found illuminate/support[v5.8.0, ..., 5.8.x-dev, v6.0.0, ..., 6.x-dev, v7.0.0, ..., 7.x-dev, v8.0.0, ..., 8.x-dev] but these were not loaded, likely because it conflicts with another require.
    - Root composer.json requires shivella/laravel-bitly ^1.1 -> satisfiable by shivella/laravel-bitly[1.1.0, ..., 1.1.11].

Any idea how to fix this?

Make `BitlyServiceProvider` deferrable

BitlyServiceProvider should be deferrable, allowing its lazy load during application execution.

It is unlikely every project page requires short link generation, thus loading service provider for it all the time is unefficient.

Illuminate\Contracts\Support\DeferrableProvider interface should be used.

Update to work with Laravel 8.x

Hi,

The latest Laravel version wants guzzle ^7.0.1:

https://github.com/laravel/laravel/blob/c6c41f11b8ea3065f035fbb6a6f570b7bfba0037/composer.json#L14

But the current version of the library has:

"guzzlehttp/guzzle": "~6.0 || ~7.0"

So when trying to run a composer update we get:

    - Installation request for laravel/framework ^8.0 -> satisfiable by laravel/framework[8.x-dev, v8.0.0, v8.0.1, v8.0.2, v8.0.3, v8.0.4, v8.1.0, v8.2.0].
    - Installation request for shivella/laravel-bitly ^1.0 -> satisfiable by shivella/laravel-bitly[1.0.0, 1.0.1, 1.0.2, 1.0.3, 1.0.4, 1.0.5, 1.0.6, 1.0.7, 1.0.8, 1.1.0, 1.1.1, 1.1.2, 1.1.3, 1.1.4, 1.1.5, 1.1.6, 1.1.7].
    - Can only install one of: guzzlehttp/guzzle[7.0.1, 6.5.x-dev].
    - Can only install one of: guzzlehttp/guzzle[7.0.x-dev, 6.5.x-dev].
    - Can only install one of: guzzlehttp/guzzle[7.1.x-dev, 6.5.x-dev].
    - Conclusion: install guzzlehttp/guzzle 6.5.x-dev
    - Installation request for guzzlehttp/guzzle ^7.0.1 -> satisfiable by guzzlehttp/guzzle[7.0.1, 7.0.x-dev, 7.1.x-dev].

An easy fix will probably change it to "^7.0.1".

Guzzle Version 7

Hi,

Currently only Guzzle Version 6 is in the composer. Can version 7 be added?

"~6.0 || ~7.0"

Url should be encoded

The URL which is returned by the plugin is not processing query strings correctly.

These should be escaped by using urlencode($url).

Laravel 11 Support

I saw you added Laravel 11 Support but you didn't tag the release. Can you please tag it?

Could not resolve host: api-ssl.bitly.com

Hi! Nice package but I'm getting this error:

InvalidResponseException in /var/www/laravel-back/vendor/shivella/laravel-bitly/src/Shivella/Bitly/Client/BitlyClient.php line 79: cURL error 6: Could not resolve host: api-ssl.bitly.com (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)

Any clue what's happening?

Thanks in advance

Simplify project directory structure

Project directory structure is overcomplicated without purpose.

The 'src' directory contains redundant folders "Shivella" / "Bitly" before getting to the actual source files.
The same happens in "Tests" directory.

Instead of usage "psr-0" in composer and redundant folders it is better to switch to "psr-4" removing extra folders in the path:

{
    "autoload": {
        "psr-4": {
            "Shivella\Bitly\\": "src"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Shivella\Bitly\\Test\\": "Tests"
        }
    },
}

Note: such changeset should be applied after other PRs are processed.

Better exceptions

Current exception hierarchy used in the project has a little sense and provide poor debug ability.

I suggest HTTP response acquired from API should be always stored in exception object, allowing developer to see exact error message.

Also converting every specific exception into a single InvalidResponseException serves no purpose.
It is better to create a base class for InvalidResponseException and AccessDeniedException and allow their throwing without global try...catch at getUrl() method.

Call to a member function getStatusCode() on null

Hi,

I performed all the necessary steps with a laravel 8 environment. I get this message when I run $bitlyUrl = app('bitly')->getUrl($curr_url);

Call to a member function getStatusCode() on null

When I try with the second solution : use Bitly;
$url = Bitly::getUrl('https://www.google.com/'); // http://bit.ly/nHcn3

I have an error in the editor :
Non static method 'getUrl' should not be called statically

any idea?
Thanks

Add support for `BitlyClient` DI binding

Package service provider should be adjusted allowing DI binding for BitlyClient class.

E.g. if I use type restriction for BitlyClient in controller or other class constructor, it should be resolved by DI. For example:

class SomeController extends Controller
{
    public function someAction(BitlyClient $bitly)
    {
        return $bitly->getUrl(...);
    }
}

PHPUnit warnings.

Running PHPUnit produces several warnings in console:

vendor/bin/phpunit 
PHPUnit 8.5.2 by Sebastian Bergmann and contributors.

  Warning - The configuration file did not pass validation!
  The following problems have been detected:

  Line 12:
  - Element 'phpunit', attribute 'syntaxCheck': The attribute 'syntaxCheck' is not allowed.

  Line 37:
  - Element 'log', attribute 'logIncompleteSkipped': The attribute 'logIncompleteSkipped' is not allowed.

  Test results may not be as expected.


Error:         No code coverage driver is available

......                                                              6 / 6 (100%)

Time: 51 ms, Memory: 6.00 MB

I suppose unsupported and unused configuration should be removed from 'phpunit.xml.dist'.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.